As the first step of a larger process, I’m trying to get the column names from some .dat files. I’m getting the “FileNotFoundError: [Errno 2] No such file or directory:” error, but I’m passing it the directory path and it is telling me the first file in the folder, so it doesn’t seem to be the usual suspect of the relative/absolute path issue.

dir_path = “C:/Projects/acs5-2022/Datasource/TestSample”

column_list = []

for path, subs, file_names in os.walk(dir_path):
for file in file_names:
datafile = pandas.read_csv(file, delimiter=’|’)
for col in datafile.columns:
col.append(column_list)

print(column_list)

And I get:
FileNotFoundError: [Errno 2] No such file or directory: ‘acsdt5y2022-b02009.dat’

acsdt5y2022-b02009.dat is the first file in the TestSample folder. I’m not giving it that information, so it can, in fact, see the files.

The header in this particular file is GEO_ID|B02009_E001|B02009_M001

I’ve tried various formats for the path directory – “C:/…..”, r”C:…..”, “C:…..” – it doesn’t seem to matter.

Any suggestions? Is it the .dat format? Is it the pipes? I’m using PyCharm and I’ve seen some posts on that, but again, I’m specifying the directory.

Thank you in advance for any direction you can give me!

New contributor

DebH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Welcome to StackOverflow. If you ask questions including code, please format the code as such via the provided “Blockquote” or “Code Cample” options. So your code is displayed like this:

dir_path = "C:/Projects/acs5-2022/Datasource/TestSample"

column_list = []

for path, subs, file_names in os.walk(dir_path): 
    for file in file_names: 
        datafile = pandas.read_csv(file, delimiter='|') 
    for col in datafile.columns: col.append(column_list)

print(column_list)

Regarding your question: file_names is only the filename without the path. read_csv() requires the full filename including the path. The path is returned in the path variable. You need to join them via os.path.join()

So:

dir_path = "C:/Projects/acs5-2022/Datasource/TestSample"

column_list = []

for path, subs, file_names in os.walk(dir_path): 
    for file in file_names: 
        full_file_path = os.join(path, file)
        datafile = pandas.read_csv(full_file_path, delimiter='|') 
    for col in datafile.columns: col.append(column_list)

print(column_list)

should work.