2

I am trying to write a script which will list down all subdirectories in a directory into a txt file.

this script will run every 1 hour through cron job so that i can append to the txt file already created in previous run and add new subdir names.

For eg:

/Directory
  /subdir1
  /subdir2
  /subdir3

txt.file should have following columns:

subdir_name     timestamp   first_filenamein_thatSUBDIR
subdir1         2015-23-12  abc.dcm
subdir2         2014-23-6   ghj.nii
.
.
.

I know to get list of directories using os.listdir but don't know how to approach this problem as i want to write same txt file with new names. ANy idea how should i do that in python?

EDit: With os.listdir i am getting sub directories name but not the time stamp. And other problem is how can i create two columns one with sub directory name and other with its time stamp as shown above?

With @Termi's help i got this code working:

import time
import os
from datetime import datetime
parent_dir = '/dicom/'

sub_dirs = os.walk(parent_dir).next()[1]

with open('exam_list.txt','a+') as f:
    lines = f.readlines()
    present_dirs = [line.split('\t')[0] for line in lines]
    for sub in sub_dirs[1:len(sub_dirs)]:
    sub = sub + '/0001'
        latest_modified = os.path.getctime(os.path.join(parent_dir,sub))
        if sub not in present_dirs and time.time() - latest_modified < 4600 :
            created = datetime.strftime(datetime.fromtimestamp(latest_modified),'%Y-%d-%m')
            file_in_subdir = os.walk(os.path.join(parent_dir,sub)).next()[2][1]
            f.write("%s\t%s\t%s\n"%(sub,created,file_in_subdir))

This code, when typed on python terminal, works well with all the variables sub, created, file_in_subdir holding some value, however, is not able to write it in a file mentioned at the beginning of the code.

I also tried if file writing is a problem using following code:

with open('./exam_list.txt','a+') as f:
    f.write("%s\t%s\n"%(sub,file_in_subdir))

Above two lines creates file properly as i intended..

Not able to point out what is the error.

3 Answers3

1
with open('some.txt', 'a') as output:
    output.write('whatever you want to add')

Opening a file with 'a' as a parameter appends everything you write to it to its end.

janrn
  • 80
  • 10
1

You can use walk from os package. It's more better than listdir. You can read more about it here En Example:


import os
from os.path import join, getctime

with open('output.txt', 'w+') as output:

    for root, dirs, files in os.walk('/Some/path/'):
        for name in files:
            create_time = getctime(join(root, name))
            output.write('%s\t%s\t%s\n' % (root, name, create_time))
mrvol
  • 2,575
  • 18
  • 21
  • but my problem of creating a list of subdirectory with time stamp is not addressed with os.walk. and above that my intention is to append the txt file with all new directories – learnningprogramming Dec 21 '15 at 15:56
  • Yes that is super helpful. I am trying to interpret python time stamp as they altogether diff from one used in Linux. every file has time stamp something like :1440697564.0 which is hard to translate to date and time – learnningprogramming Dec 21 '15 at 17:07
1

To get the immediate sub-directories in the parent directory use os.walk('path/to/parent/dir').next()[1].

os.walk().next() gives a list of lists as [current_dir, [sub-dirs], [files] ] so next()[1] gives sub-directories

opening the file with 'a+' will allow you to both read and append to the file. Then store the sub-directories that are already in the file

with open('dirname.txt','a+') as f:
    lines = f.readlines()
    present_dirs = [line.split('\t')[0] for line in lines]

Now for each sub-directory check whether it is already present in the list and if not, add it to the file. If you execute it every hour you can even check for new files created(or modified in linux systems) in the last hour by using getctime

time.time() - os.path.getctime(os.path.join(parent_dir,sub)) < 3600

Now for any new sub-directory use os.walk('path/to/subdir').next[2] and get the filenames inside

import time
import os
from datetime import datetime
parent_dir = '/path/to/parent/directory'

sub_dirs = os.walk(parent_dir).next()[1]

with open('dirname.txt','a+') as f:
    lines = f.readlines()
    present_dirs = [line.split('\t')[0] for line in lines]
    for sub in sub_dirs:

        latest_modified = os.path.getctime(os.path.join(parent_dir,sub))

        if sub not in present_dirs and time.time() - latest_modified < 3600 :

            created = datetime.strftime(datetime.fromtimestamp(latest_modified),'%Y-%d-%m')
            file_in_subdir = os.walk(os.path.join(parent_dir,sub)).next()[2][0]
            f.write("%s\t%s\t%s\n"%(sub,created,file_in_subdir))
Termi
  • 641
  • 7
  • 14
  • file_in_subdir is returning .DS_store file.. instead of which i am looking for 3rd file in that sub directory .. i tried .next()[2][2] but it gives me error. .. how can i look for nth file using os.walk.next()[][] ..? Any help is appreciated – learnningprogramming Jan 05 '16 at 15:31
  • 1
    the result list of `os.walk(dir)` contains files and subfolders in **arbitrary order** . So the 3rd file you get in `next()[2][2]` may not be one you want. If you want 3rd file in alphabetical order then sort the list and take the 3rd element. And for your error, If you are getting `IndexError` it means you don't have a 3rd file in one of the subfolders. – Termi Jan 06 '16 at 08:01
  • i tried your above code but there seems to be problem.. i have edited my question to explain it in detail – learnningprogramming Jan 19 '16 at 21:13
  • i am for some reason not able to write to the file. I have edited the question and things what i tried .. can you help with this? – learnningprogramming Jan 27 '16 at 16:12
  • hey sorry for late reply.. are you getting any error or just the file is empty ? if file is empty try after removing `time.time() - latest_modified < 4600` – Termi Feb 03 '16 at 12:24