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:
What I'm looking for:
DirName,Filename
999,123.tif
999,1234.tif
999,abc.tif
900,1236.tif
900,xyz.tif
...etc
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.
My current code looks like:
Update
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)?
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()
Thank You
Maybe this helps:
You could get the absolute file paths and then do the following
import os.path
p = "/tmp/999/123.tif"
dir, filename = os.path.basename(os.path.dirname(p)), os.path.basename(p)
Result:
In [21]: dir
Out[21]: '999'
In [22]: filename
Out[22]: '123.tif'
Also consider using csv module to write this kind of files.
import csv
import os.path
# You already have a list of absolute paths
files = ["/tmp/999/123.tif"]
# csv writer
with open('/tmp/out.csv', 'wb') as out_file:
csv_writer = csv.writer(out_file, delimiter=',')
csv_writer.writerow(('DirName','Filename'))
for f in files:
csv_writer.writerow((os.path.basename(os.path.dirname(f)),
os.path.basename(f)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With