Home > Software design >  'No such file or directory' error when writing a CSV file
'No such file or directory' error when writing a CSV file

Time:05-07

There are a couple of questions on this topic (eg. here or here ), but none of these solutions work for me.

I want to write a CSV file using python. My current (super basic) code:

 filename="/home/Project/Clean/outputFiles/"some_name.csv"
 with open(filename,'w') as outputfile:
        writer=csv.writer(outputfile)
        writer.writerow(header)
        writer.writerows(data)

data and header are two lists.

I get the error message:

FileNotFoundError                         Traceback (most recent call last)
/home/Project/Clean/mytestfile.py in <module>
    132 header=['run_id','rep','initial','bounds','history']
    133 data=zip(Run_id,Reps, Initials,Bounds,Histories)
--> 134 with open(filename,'w') as outputfile:
    135     writer=csv.writer(outputfile)
    136     writer.writerow(header)

FileNotFoundError: [Errno 2] No such file or directory: 'home/Project/Clean/outputFiles/none/some_name.csv'

The file does not exist before. This should not be a matter of permissions, because I can write a different file from the same script to the same directory without errors. I can not write the file from another script. The data should not be the problem, because I have written files with data in this format to other files and the same problem occurs when inserting some dummy instead.

CodePudding user response:

I think you are using lynuix judjing by the home part of the path. However in python we use back slashes \\ and not / when using a path. This could be the source of the error.

CodePudding user response:

Doublecheck path you use. In Traceback you have following. Pay attention to none before some_name

No such file or directory: 'home/Project/Clean/outputFiles/none/some_name.csv'

But in code

filename="/home/Project/Clean/outputFiles/"some_name.csv"

Is extra quotes before some_name a typo?

  • Related