python show folder files and not subfolder files

pascal z barpasc at yahoo.com
Sun Oct 4 05:56:06 EDT 2020


On Thursday, September 24, 2020 at 4:37:07 PM UTC+2, Terry Reedy wrote:
> On 9/23/2020 7:24 PM, pascal z via Python-list wrote:
> > Please advise if the following is ok (i don't think it is)
> > 
> > #!/usr/bin/env python3
> > # -*- coding: utf-8 -*-
> > 
> > import os
> > 
> > csv_contents = ""
> > output_file = '/home/user/Documents/csv/output3csv.csv'
> > Lpath = '/home/user/Documents/'
> > 
> > csv_contents = "FOLDER PATH;Size in Byte;Size in Kb;Size in Mb;Size in Gb\n"
> > 
> > d_size = 0
> > for root, dirs, files in os.walk(Lpath, topdown=False):
> >      for i in files:
> >          d_size += os.path.getsize(root + os.sep + i)
> >      csv_contents += "%s   ;%.2f   ;%.2f   ;%.2f   ;%.2f  \n" % (root, d_size, d_size/1024, d_size/1048576, d_size/1073741824)
> > 
> >      counter = Lpath.count(os.path.sep)
> >      if counter < 5:
> >          for f in os.listdir(Lpath):
> >              path = os.path.join(Lpath, f)
> >              f_size = 0
> >              f_size = os.path.getsize(path)
> >              csv_contents += "%s   ;%.2f   ;%.2f   ;%.2f   ;%.2f  \n" % (path, f_size, f_size/1024, f_size/1048576, f_size/1073741824)
> > 
> > fp = open(output_file, "w")
> > fp.write(csv_contents)
> > fp.close()
> 
> 
> Read
> https://docs.python.org/3/faq/programming.html#what-is-the-most-efficient-way-to-concatenate-many-strings-together
> -- 
> Terry Jan Reedy

Thanks for this tip. I do think it's better to use lists than concatenate into string variable. However, writing a list to a csv file is not something easy. If strings stored into the list have commas and single quotes (like song title's), it messes up the whole csv when it first meets this. Example with arr as list:


import csv
import io

(...)

csv_contents = "%s;%s;%s;%.2f;%.2f;%.2f;%.2f;%s" % (vfolder_path, vfile_name, vfolder_path_full, 0.00, 0.00, 0.00,0.00, "folder")
arr.append([csv_contents])

b = io.BytesIO()
with open(CSV_FILE,'w', newline ='\n') as f:
    #write = csv.writer(f,delimiter=';')
    #write = csv.writer(f,quotechar='\'', quoting=csv.QUOTE_NONNUMERIC,delimiter=',')
    write = csv.writer(f,b)
    for row in arr:
        write.writerows(row)

(...)

string samples: ;'Forgotten Dreams' Mix.mp3;'Awakening of a Dream' Ambient Mix.mp3;Best of Trip-Hop & Downtempo & Hip-Hop Instrumental.mp3;2-Hour _ Space Trance.mp3

for the titles above, the easiest thing to write csv for me is 


(...)
csv_contents += "%s;%s;%s;%.2f;%.2f;%.2f;%.2f;%s" % (vfolder_path, vfile_name, vfolder_path_full, 0.00, 0.00, 0.00,0.00, "folder"

with open(CSV_FILE,'w') as f:
    f.write(csv_contents)


csv_contents can be very large and it seems to work. It can concatenate 30k items and it's ok. Also with the above, I have the expected result into each of the 8 rows having the corresponding data. This is not always the case with csv writerows. If it meets a character it can't process, from there everything go into a single cell row. The split on semi colon from doesnt work anymore.

I am not allowed to change the name of the files (it could be used later somewhere else, making side effects...).


More information about the Python-list mailing list