Python Forum

Full Version: Recursively create a file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello all I'm new to the world of python.

(please don't bully the scriptkiddo  Big Grin  )



I've wrote script that create folders in a specific location on a RAID,  updated every night to the cloud with a cronjob.
However, since the dosent' allow empty "folders", if one is not used will stay empty and won't be uploaded.
That's why I would like to add a recursive "touch" that create a hidden file, like store.oks or something similar, that allow AWSCLI (command line from amazon cloud) to properly update the whole folder structure.

Can someone help me out or point in the right direction, please?

here's a copy of the script



Thank you for your time
Strong recommendation: learn about lists and how to iterate them. You code could be around 30 lines...

To make an empty file: just use
open('filename.dat','w').close()
(note that unlike "touch" this will overwrite an existing file...)
I'll look into it, thank you.
So I can use list for variables (all the leters) for assiging the correct folder and use a loop to check if the correct folder exist?

Can I do a reiteration for have this line of code happening everytime a folder is created? Or do I need to stick it underneath every folder branch to have the file in place?

open('filename.dat','w').close()
Something like this (syntax not checked):

dirs=[
'001_DATALAB',
'001_DATALAB/001_Rushes',
'001_DATALAB/001_Rushes/001_video',
'001_DATALAB/001_Rushes/002_audio'
# add more directories here
]

# Now loop over the directories above, doing the same thing for each:
for dir in dirs:
  fulldir=os.path.join(project_path,dir)
  if not os.path.exists(fulldir):
        os.makedirs(fulldir, mode=0777)
  placeholder=os.path.join(fulldir,dir)
  open('store.oks', 'w').close()  # has to be opened in write mode... 
 
I don't really understand what you are doing with the umask, this seems completely useless
Thanks for the hints :)

I think the umask is there because this script write to an active folder shared over network and the scripts has to be run under root or an admin that can write folders with 777 permission in order to make them accessible to different users of the network.
(Nov-03-2016, 10:02 AM)digitalquake Wrote: [ -> ]Thanks for the hints :)

I think the umask is there because this script write to an active folder shared over network and the scripts has to be run under root or an admin that can write folders with 777 permission in order to make them accessible to different users of the network.

It's not safe to set permissions to 777. Instead, create a group, assign the users to this group and set the owner of the file to be user:group and set the corresponding permissions.
(Nov-03-2016, 10:02 AM)digitalquake Wrote: [ -> ]Thanks for the hints :)

I think the umask is there because this script write to an active folder shared over network and the scripts has to be run under root or an admin that can write folders with 777 permission in order to make them accessible to different users of the network.

If you use the mode explicitly and never change the umask this is pointless. What you can do is change the umask once for all at the beginning to 0000 so that it doesn't restrict the rights on the files and directories you create (0666 for files and 0777 for directories). Which means your code could be a lot simpler.
Our setup use netatalk and smb share so people have access only to limited folders, and 0777 is handy for them so they can read and write into the folders but not messup with other projects. Maybe a swtich to 0666 is not a bad idea.
(Nov-03-2016, 04:29 PM)digitalquake Wrote: [ -> ]Our setup use netatalk and smb share so people have access only to limited folders, and 0777 is handy for them so they can read and write into the folders but not messup with other projects. Maybe a swtich to 0666 is not a bad idea.

Keep 777 for folders. The "execute" bit is important. If reset, you cannot access anything under that directory.
(Nov-03-2016, 10:26 PM)Ofnuts Wrote: [ -> ]
(Nov-03-2016, 04:29 PM)digitalquake Wrote: [ -> ]Our setup use netatalk and smb share so people have access only to limited folders, and 0777 is handy for them so they can read and write into the folders but not messup with other projects. Maybe a swtich to 0666 is not a bad idea.

Keep 777 for folders. The "execute" bit is important. If reset, you cannot access anything under that directory.

I see. thanks for the tip!
Pages: 1 2