Note that there are some explanatory texts on larger screens.

plurals
  1. PODirectory Name + File Name to Text File
    text
    copied!<p>I have many directories with files in them. I want to create a comma delimited txt file showing directory name, and the files that are within that particular directory, see example below:</p> <p><strong>What I'm looking for:</strong></p> <pre><code>DirName,Filename 999,123.tif 999,1234.tif 999,abc.tif 900,1236.tif 900,xyz.tif ...etc </code></pre> <p>The python code below pushes a list of those file paths into a text file, however I'm unsure of how to format the list as described above.</p> <p><strong>My current code looks like:</strong></p> <p><strong>Update</strong> </p> <p>I've been able to format the text file now, however I'm noticing all the directory names/filenames are not being written to the text file. It is only writing ~4000 of the ~8000 dir/files. Is there some sort limit that I'm reaching with the text file number of rows, the list (mylist) size, or some bad file dir/file character that is stopping it (see updated code below)?</p> <pre><code>from os import listdir from os.path import isfile, join root = r'C:\temp' mylist = ['2'] for path, subdirs, files in os.walk(root): for name in files: mylist.append(os.path.join(path, name)) txt = open(r'C:\temp\output.txt', 'w') txt.write('dir' + ',' + 'file' + '\n') for item in mylist: list = mylist.pop(0) dir, filename = os.path.basename(os.path.dirname(list)), os.path.basename(list) txt.write(dir + ',' + filename + '\n') with open(r'C:\temp\output.txt', 'r') as f: read_data = f.read() </code></pre> <p>Thank You</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload