0

I've encountered a very bizarre error when trying to write files with python. As of today, whenever I try and open a file in write mode, python throws the error 'No such file or directory', even if the directory definitely exists. All other python functionality seems to be working, including reading files.

A simple example of the problem, when run using the command line from my documents:

>>> with open('test.csv', 'w') as f:
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'test.csv'

I'm using a windows 10 machine, python 3.7, with conda environments specific to projects I'm working on.

I have tried:

  • Restarting
  • Creating a clean conda environment (conda create --name test python=3.7)
  • Running in base without a conda environment
  • Providing the path as an absolute rather than relative, i.e. C:/Users/<myname>/Documents/test.csv
  • Trying to run this in other directories

The only success I've had is when running the code with append mode. If I try and create a file as above using mode='a' it throws the error, however if I manually create an empty file, I can append into it.

I really have no idea how to resolve this, other than to completely remove anaconda and python and re-install.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Josh Kidd
  • 816
  • 2
  • 14
  • 35
  • Try [https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory] – aartist May 01 '20 at 14:47
  • 4
    Looks like a permission problem. Check who is the owner of the Python executable and whether there are OS restrictions on it. – norok2 May 01 '20 at 14:48
  • Does the account executing the script have sufficient permissions to create files in the current directory? – esqew May 01 '20 at 14:51
  • @aartist os.listdir successfully lists the files in the directory. – Josh Kidd May 01 '20 at 14:52
  • @norok2 My user account has full read/write permissions on the python executable and the target directory – Josh Kidd May 01 '20 at 14:55
  • What is the output of `os.access()`? https://stackoverflow.com/q/2113427/5218354 – norok2 May 01 '20 at 14:56
  • @norok2 os.access('C:/Users//documents/', os.W_OK) returns True, Is that what you meant? – Josh Kidd May 01 '20 at 15:35
  • @r.ook how would I check if the python executable no longer has permission to create files? – Josh Kidd May 01 '20 at 15:42
  • Also double check your directory names are correct: https://stackoverflow.com/questions/31414263/python-using-open-w-filenotfounderror checking `os.getcwd()` would be important if you're not using absolute path. – r.ook May 01 '20 at 15:45
  • 1
    Does this answer your question? [open file in "w" mode: IOError: \[Errno 2\] No such file or directory](https://stackoverflow.com/questions/2401628/open-file-in-w-mode-ioerror-errno-2-no-such-file-or-directory) – r.ook May 01 '20 at 15:46
  • Path is definitely correct, and the problem exists even when trying to write to the current directory, as in the example above. Tried both absolute and relative paths – Josh Kidd May 01 '20 at 15:53
  • Can you just create *any* file in the current directory? Are you using an IDE or just the `cmd.exe` window? How about a simple "echo >test.csv" from the C: prompt? – Mark Tolonen May 01 '20 at 16:09
  • I can right click and create a text file in the directory, however the command echo hello > test.txt results in a "system can't find the file specified" error. I'm using the anaconda prompt. – Josh Kidd May 01 '20 at 17:16
  • Is "C:/Users//Documents/test.csv" the real path, except for ""? If not please use a path that's at least almost identical -- i.e. retain all non-ASCII, non-alphanumeric characters and the exact length of the path, even if you change some of the names for privacy or proprietary reasons. – Eryk Sun May 01 '20 at 22:53
  • It's a little redundant I guess blanking out my name as it's written below all my answers. The exact path is as I stated above, with being "joshua.kidd" – Josh Kidd May 03 '20 at 09:02

1 Answers1

-1

try declare a variable for the file before 'with' statement:

file = open('test.csv', 'w')
with file as f:
     # your code here