Python Forum
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
Reply
#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
Reply
#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()
Reply
#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
Reply
#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.
Reply
#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
Reply
#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
Reply
#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.
Reply
#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
Reply
#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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Choices from .ods file columns cspower 3 519 Dec-28-2023, 09:59 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,784 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  Use PM4PY and create working file thomaskissas33 0 573 Nov-14-2023, 06:53 AM
Last Post: thomaskissas33
  Create csv file with 4 columns for process mining thomaskissas33 3 695 Nov-06-2023, 09:36 PM
Last Post: deanhystad
  create exe file for linux? korenron 2 909 Mar-22-2023, 01:42 PM
Last Post: korenron
  my first file won't create itself MehHz2526 2 860 Nov-27-2022, 12:58 AM
Last Post: MehHz2526
  Create multiple/single csv file for each sql records mg24 6 1,323 Sep-29-2022, 08:06 AM
Last Post: buran
Sad pandas writer create "corrupted" file freko75 1 2,737 Jun-14-2022, 09:57 PM
Last Post: snippsat
  How to return the next page from json recursively? sandson 0 1,104 Apr-01-2022, 11:01 PM
Last Post: sandson
  create new log file on logging? korenron 6 2,204 Mar-22-2022, 07:14 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020