Reputation: 41
My OS is Windows, newest version and all updated, I think the issue lies in the path or something to do with OneDrive.
Using the code:
file = "dataset.csv"
with open(file, "w") as f:
f.write(data)
I get the error: FileNotFoundError: [Errno 2] No such file or directory: 'dataset.csv'
, but this is an error I have never had before...
I have enabled long paths in the Windows registry and rebooted and it still does not work.
However, I have tried to run this code in different paths of my pc, and it does work if I run it in the path or with a shorter path than: C:\Users\user\OneDrive - UniversityX XXXXXXXXXX XXXXXXXX\XXXXXXXXX
, where X is also part of the folder name as it is my unversity`s name.
If I run it after that path it just stops working and outputs that error every time I run the code in VSCode.
Update: This does not only not work in VSCode, but if I use matlab or any other program to save a file, it does not let me save the file either as it says that the file cannot be found.
Does anyone know how I can fix this, please?
Upvotes: 1
Views: 102
Reputation: 41
I found out that it was a Windows Security issue in the end... I enabled controlled folder access. To disable this follow these steps:
This option apparently blocks any program from creating new files in certain directories.
Upvotes: 3
Reputation: 33343
FileNotFoundError: [Errno 2] No such file or directory: 'dataset.csv'
The only way to get that error is if you're creating a file in a directory that doesn't exist.
The filename dataset.csv
does not refer to any subdirectories, so it defaults to creating the file in the current directory.
So, it must be that the current directory somehow no longer exists.
Upvotes: 0
Reputation: 365
If you define folder / file path in python like this
"C:\Users\user\OneDrive - UniversityX XXXXXXXXXX XXXXXXXX\XXXXXXXXX"
you should check for escape characters. I don't know if it's you're case, ofc, but keep in minds things like "\t"
, "\n"
and many others have a special meaning.
To avoid this use
"C:\\Users\\user\\OneDrive - UniversityX XXXXXXXXXX XXXXXXXX\\XXXXXXXXX"
or
"C:/Users/user/OneDrive - UniversityX XXXXXXXXXX XXXXXXXX/XXXXXXXXX"
(may depend on your application)
More infos: https://www.w3schools.com/python/gloss_python_escape_characters.asp
Upvotes: 0