2

I'm trying to generate a .csv file that lists all .txt file in a sub-directory but can exclude a .txt file of a certain size.

Without an if statement around file size, I can successfully generate the .csv file list that has the full paths to the .txt files (1105 files total)

import os
import csv
import os.path
mtn_path = "TEXT_FILE_PATH\\"
a = open(mtn_path + "output.csv", 'w+', newline='')
num_files = 0
for path, dirnames, filenames in os.walk(mtn_path):
    for filename in [f for f in filenames if f.endswith(".txt")]:
#       if os.stat(filename).st_size > 20000:
            f = os.path.join(path, filename)
            a.write(str(f) + os.linesep)
            num_files = num_files + 1
)
#
print("The total number of text files found was " + str(num_files))

When I include the if os.stat line, I get the error:

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'INT_NR_H1000Cu_000.mtn_001.txt'

That particular .txt file is the first one listed in the csv file of the working code.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
callin
  • 49
  • 8
  • 2
    `filename` is just the file name, you need to pass the full path to `os.stat`. – Blorgbeard May 29 '19 at 20:37
  • Thank you. I replaced the code and used "f" in the if os.path file and placed it before the .write command and it looks like it's working now; down to the 553 files as hoped. – callin May 29 '19 at 20:41

2 Answers2

4

You need to pass the full path to os.stat. Rearrange your code a bit:

for filename in [f for f in filenames if f.endswith(".txt")]:
    f = os.path.join(path, filename)
    if os.stat(f).st_size > 20000:
        a.write(str(f) + os.linesep)
        num_files = num_files + 1
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

I think you have to include the full path in the if statement. os.path can't find filename

Add full path to the file os.path.join(path, filename)

Also delete that slash at the end of TEXT_FILE_PATH

import os
import csv
import os.path
mtn_path = r"TEXT_FILE_PATH"
a = open(mtn_path + "output.csv", 'w+', newline='')
num_files = 0
for path, dirnames, filenames in os.walk(mtn_path):
    for filename in [f for f in filenames if f.endswith(".txt")]:       
        if os.stat(os.path.join(path, filename)).st_size > 20000:
            f = os.path.join(path, filename)
            a.write(str(f) + os.linesep)
            num_files = num_files + 1
)
#
print("The total number of text files found was " + str(num_files))
joe_schmoe
  • 1
  • 1
  • 1