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?
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?
Share Improve this question edited Nov 21, 2024 at 8:53 Coolgolf asked Nov 20, 2024 at 16:01 CoolgolfCoolgolf 411 silver badge5 bronze badges 03 Answers
Reset to default 3I found out that it was a Windows Security issue in the end... I enabled controlled folder access. To disable this follow these steps:
- Settings > Update & Security (Windows 10) or Privacy & Security (Windows 11) > Windows Security > Virus & threat protection.
- Under Virus & threat protection settings, select Manage settings.
- Under Controlled folder access, select Manage Controlled folder access.
- Switch the Controlled folder access setting to Off.
This option apparently blocks any program from creating new files in certain directories.
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/python/gloss_python_escape_characters.asp
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.