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
Apologise, but with


Quote:#       DATALAB BRANCH
                a       = os.path.join(project_path, '001_DATALAB')
                 open('001_DATALAB/test.txt', 'w').close()



I get


Error:
 File "OKAYfolder.py", line 13     open('001_DATALAB/test.txt', 'w').close()     ^ IndentationError: unexpected indent
(Nov-14-2016, 03:44 PM)digitalquake Wrote: [ -> ]Apologise, but with


Quote:#       DATALAB BRANCH
                a       = os.path.join(project_path, '001_DATALAB')
                 open('001_DATALAB/test.txt', 'w').close()



I get


Error:
 File "OKAYfolder.py", line 13     open('001_DATALAB/test.txt', 'w').close()     ^ IndentationError: unexpected indent

it appears you have a single whitespace before open() that does not exist on the line before it.
Ok, now that my grammar nazi attitude towards python is fixed, I'm still not getting any output file in the folders.
I don't get any debug error but simply nothing happens.

I tried different ways, specifing folders paths, different way to implement open, as well as different scripts in order to see if the open file makes something but no joy. Is it because the script is trying to write a file at the same time it write the folder?



EDIT: see top or bottom part of the script.


#!/usr/bin/python

print 'Running OKAYfolder script. This script will generate the standard tree structure for OKAYstudio scripts (projects)'

while True:
    import os
    aw_directory = "/mnt/sdb1/_Active Working/9999_testfolder"
    for child in os.listdir(aw_directory):
        project_path = os.path.join(aw_directory, child)
        if os.path.isdir(project_path):
#    DATALAB BRANCH
        a     = os.path.join(project_path, '001_DATALAB')
        open('file.txt', 'w').close()
        b    = os.path.join(project_path, '001_DATALAB/001_Rushes')
        c    = os.path.join(project_path, '001_DATALAB/001_Rushes/001_video')
        d    = os.path.join(project_path, '001_DATALAB/001_Rushes/002_audio')
                e    = os.path.join(project_path, '001_DATALAB/002_Transcodes')
                f    = os.path.join(project_path, '001_DATALAB/003_Encodes')
                g    = os.path.join(project_path, '001_DATALAB/004_Artwork-Titles-Graphics')
                h    = os.path.join(project_path, '001_DATALAB/005_Music-Sound')
#       EDITORIAL BRANCH
        i    = os.path.join(project_path,'002_EDITORIAL')
        j    = os.path.join(project_path,'002_EDITORIAL/001_offline/')
        k    = os.path.join(project_path,'002_EDITORIAL/001_offline/001_exports')
        l    = os.path.join(project_path,'002_EDITORIAL/001_offline/002_assets')
        m    = os.path.join(project_path,'002_EDITORIAL/002_online')
        n    = os.path.join(project_path,'002_EDITORIAL/002_online/001_exports')
        o    = os.path.join(project_path,'002_EDITORIAL/002_online/002_assets')
#       ANIMATION BRANCH
        p    = os.path.join(project_path,'003_ANIMATION')    
        q    = os.path.join(project_path,'003_ANIMATION/001_exports')
        r    = os.path.join(project_path,'003_ANIMATION/002_assets')
#       GRADE BRANCH
        s    = os.path.join(project_path,'004_GRADE')
        t    = os.path.join(project_path,'004_GRADE/001_exports')
        u    = os.path.join(project_path,'004_GRADE/002_assets')
#       VFX BRANCH
        v    = os.path.join(project_path,'005_VFX')
        w    = os.path.join(project_path,'005_VFX/001_exports')
        x    = os.path.join(project_path,'005_VFX/002_assets')
#       REF&TREATMENTS BRANCH
        y    = os.path.join(project_path,'006_REF-TREATMENTS')
#       DELIVERABLES BRANCH
        z    = os.path.join(project_path,'007_DELIVERABLES')
        aa    = os.path.join(project_path,'007_DELIVERABLES/001_WIP')
        ab    = os.path.join(project_path,'007_DELIVERABLES/001_WIP/001_offline')
        ac    = os.path.join(project_path,'007_DELIVERABLES/001_WIP/002_animation')
        ad    = os.path.join(project_path,'007_DELIVERABLES/001_WIP/003_grade')
        ae    = os.path.join(project_path,'007_DELIVERABLES/001_WIP/004_VFX')
        af    = os.path.join(project_path,'007_DELIVERABLES/001_WIP/005_online')
        ag    = os.path.join(project_path,'007_DELIVERABLES/002_Master')
        ah    = os.path.join(project_path,'007_DELIVERABLES/002_Master/encodes')

#       DATALAB VARIABLES
        if not os.path.exists(a):
            original_umask_a = os.umask(0)            
            os.makedirs(a, mode=0777)
            os.umask(original_umask_a)
                if not os.path.exists(b):
                    original_umask_b = os.umask(0)
                    os.makedirs(b, mode=0777)
                    os.umask(original_umask_b)
                if not os.path.exists(c):
                    original_umask_c = os.umask(0)
                    os.makedirs(c, mode=0777)
                    os.umask(original_umask_c)
                if not os.path.exists(d):
                    original_umask_d = os.umask(0)
                    os.makedirs(d, mode=0777)
                    os.umask(original_umask_d)
                if not os.path.exists(e):
                    original_umask_e = os.umask(0)
                    os.makedirs(e, mode=0777)
                    os.umask(original_umask_e)
                if not os.path.exists(f):
                    original_umask_f = os.umask(0)
                    os.makedirs(f, mode=0777)
                    os.umask(original_umask_f)
                if not os.path.exists(g):
                    original_umask_g = os.umask(0)
                    os.makedirs(g, mode=0777)
                    os.umask(original_umask_g)
                if not os.path.exists(h):
                    original_umask_h = os.umask(0)
                    os.makedirs(h, mode=0777)
                    os.umask(original_umask_h)
#       EDITORIAL VARIABLES
                if not os.path.exists(i):
                    original_umask_i = os.umask(0)
                    os.makedirs(i, mode=0777)
                    os.umask(original_umask_i)
                if not os.path.exists(j):
                    original_umask_j = os.umask(0)
                    os.makedirs(j, mode=0777)
                    os.umask(original_umask_j)
                if not os.path.exists(k):
                    original_umask_k = os.umask(0)
                    os.makedirs(k, mode=0777)
                    os.umask(original_umask_k)
                if not os.path.exists(l):
                    original_umask_l = os.umask(0)
                    os.makedirs(l, mode=0777)
                    os.umask(original_umask_l)
                if not os.path.exists(m):
                    original_umask_m = os.umask(0)
                    os.makedirs(m, mode=0777)
                    os.umask(original_umask_m)
                if not os.path.exists(n):
                    original_umask_n = os.umask(0)
                    os.makedirs(n, mode=0777)
                    os.umask(original_umask_n)
                if not os.path.exists(o):
                    original_umask_o = os.umask(0)
                    os.makedirs(o, mode=0777)
                    os.umask(original_umask_o)
#       ANIMATION VARIABLES
                if not os.path.exists(p):
                    original_umask_p = os.umask(0)
                    os.makedirs(p, mode=0777)
                    os.umask(original_umask_p)
                if not os.path.exists(q):
                    original_umask_q = os.umask(0)
                    os.makedirs(q, mode=0777)
                    os.umask(original_umask_q)
                if not os.path.exists(r):
                    original_umask_r = os.umask(0)
                    os.makedirs(r, mode=0777)
                    os.umask(original_umask_r)
#       GRADE VARIABLES
                if not os.path.exists(s):
                    original_umask_s = os.umask(0)
                    os.makedirs(s, mode=0777)
                    os.umask(original_umask_s)
                if not os.path.exists(t):
                    original_umask_t = os.umask(0)
                    os.makedirs(t, mode=0777)
                    os.umask(original_umask_t)
                if not os.path.exists(u):
                    original_umask_u = os.umask(0)
                    os.makedirs(u, mode=0777)
                    os.umask(original_umask_u)
#       VFX VARIABLES
                if not os.path.exists(v):
                    original_umask_v = os.umask(0)
                    os.makedirs(v, mode=0777)
                    os.umask(original_umask_v)
                if not os.path.exists(w):
                    original_umask_w = os.umask(0)
                    os.makedirs(w, mode=0777)
                    os.umask(original_umask_w)
                if not os.path.exists(x):
                    original_umask_x = os.umask(0)
                    os.makedirs(x, mode=0777)
                    os.umask(original_umask_x)
#       REF&TREATMENTS VARIABLES
                if not os.path.exists(y):
                    original_umask_y = os.umask(0)
                    os.makedirs(y, mode=0777)
                    os.umask(original_umask_y)
#       DELIVERABLES VARIABLES
                if not os.path.exists(z):
                    original_umask_z = os.umask(0)
                    os.makedirs(z, mode=0777)
                    os.umask(original_umask_z)
                if not os.path.exists(aa):
                    original_umask_aa = os.umask(0)
                    os.makedirs(aa, mode=0777)
                    os.umask(original_umask_aa)
                if not os.path.exists(ab):
                    original_umask_ab = os.umask(0)
                    os.makedirs(ab, mode=0777)
                    os.umask(original_umask_ab)
                if not os.path.exists(ac):
                    original_umask_ac = os.umask(0)
                    os.makedirs(ac, mode=0777)
                    os.umask(original_umask_ac)
                if not os.path.exists(ad):
                    original_umask_ad = os.umask(0)
                    os.makedirs(ad, mode=0777)
                    os.umask(original_umask_ad)
                if not os.path.exists(ae):
                    original_umask_ae = os.umask(0)
                    os.makedirs(ae, mode=0777)
                    os.umask(original_umask_ae)
                if not os.path.exists(af):
                    original_umask_af = os.umask(0)
                    os.makedirs(af, mode=0777)
                    os.umask(original_umask_af)
                if not os.path.exists(ag):
                    original_umask_ag = os.umask(0)
                    os.makedirs(ag, mode=0777)
                    os.umask(original_umask_ag)
                if not os.path.exists(ah):
                    original_umask_ah = os.umask(0)
                    os.makedirs(ah, mode=0777)
                    os.umask(original_umask_ah)

#       THE FOLLOWING LINES WILL CREATE A hidden "store.dat" FILE THAT WILL LET AWS S3 UPLOAD THE ENTIRE FOLDER STRUCTURE


#def touch(path):
 #   with open(path, 'w'):
  #      os.utime(path, None)


#open('001_DATALAB/file.txt', 'w').close()


# Open a file
fobj = open("001_DATALAB/store.dat", "w")
fobj.write( "Python test");

# Close opend file
fobj.close()
#open('001_DATALAB/file.txt', 'w').close()
Everthought about using functions?

def createdir(d):
    original_umask_p = os.umask(0)
    os.makedirs(d, mode=0777)
    os.umask(original_umask_p)
But you can set the umask() for the whole app on startup. And no need to restore it since it is set only for the process.
(Nov-25-2016, 02:52 PM)Ofnuts Wrote: [ -> ]Everthought about using functions?

def createdir(d):
    original_umask_p = os.umask(0)
    os.makedirs(d, mode=0777)
    os.umask(original_umask_p)
But you can set the umask() for the whole app on startup. And no need to restore it since it is set only for the process.

Sorry Ofnuts but I can't see how that tell me why the open() line I wrote won't work in my code Confused
I didn't see your report of the error (stack trace, etc...)

IMHO, since we are on the 15th post... you aren't going anywhere because you are still very much lacking a development and debug method.

  1. Start small.
  2. Remove (and not comment out, you need to look at a small source file) all directories but one.  
  3. Make something that creates one directory and the file in it.
  4. Check it works.
  5. Change it to use a function that creates a directory you pass as a parameter
  6. Check that it still works
  7. Add a list of three directories
  8. Iterate the list and call the function on each element
  9. Check that it still works
  10. Replace the list by the complete list of directories
  11. That should work.
Notice that you aren't adding much code between 5 and 8, and not adding any between 8 and the end (the list of directories is just data).

Incidentally, the function you use is os.makedirs(). This 's' is not a coincidence... it creates all the required intermediate directories so some of your directory creations are superfluous. To wit:

Quote:os.makedirs(path[, mode])

Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory. Raises an error exception if the leaf directory already exists or cannot be created. The default mode is
0777 (octal).

... which also means that your mode setting is superfluous as well.
I see will start from scratch that's the best way.

Thank you
(Nov-25-2016, 03:46 PM)digitalquake Wrote: [ -> ]Sorry Ofnuts but I can't see how that tell me why the open() line I wrote won't work in my code Confused

Maybe not, but you have the exact three lines of code repeated over and over again, which is a problem functions can fix. ...and that'll make it easier to read, so random people like us can just glance at your code to see if there's an issue, instead of intently studying it for minor changes from block to block.
Well I'm not a programmer I'm just trying to have a file created at the same time with the folder that's it. I'm not really able and have the mindset to keep up with any programmer Big Grin
Quote:Well I'm not a programmer I'm just trying to have a file created

You're writing a program, why not do it correctly
Pages: 1 2