Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Recursively create a file
#1
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
Quote
#2
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...)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Quote
#3
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()
Quote
#4
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
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Quote
#5
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.
Quote
#6
(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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#7
(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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Quote
#8
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.
Quote
#9
(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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Quote
#10
(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!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Find a given file recursively inside a directory alinaveed786 1 358 Jul-01-2019, 01:53 PM
Last Post: ichabod801
  Create file archive that contains crypto hash ED209 1 203 May-29-2019, 03:05 AM
Last Post: heiner55
  Call functions recursively in tree structure using python dubru 1 301 Feb-15-2019, 06:43 PM
Last Post: nilamo
  Python Directory and File Create Error basic_learner 1 470 Dec-16-2018, 11:03 AM
Last Post: buran
  Help on adding two lists recursively Metalman488 3 556 Sep-16-2018, 08:10 PM
Last Post: Metalman488
  How can Read text file and create array? puneet102 1 612 May-22-2018, 06:41 PM
Last Post: snippsat
  scandir() recursively and return path + filename malonn 6 3,294 May-09-2018, 03:45 PM
Last Post: wavic
  create objects from file faizahsan66 3 928 Aug-24-2017, 11:38 AM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)