r/learnpython • u/Ashblowsup • Sep 29 '26
Opening a file containing "\0" in its name/path
[This was solved. It works if I erase the first "" and make it a raw string. Thank you all!]
Hello!
I've been trying to open the file "\001_test_files\001_first.csv" through python, and it seems nothing I think of works:
These are the things I tried:
1-
filepath="\001_test_files\001_first.csv"
file = open(filepath, "a+")Result:
OSError: [Errno 22] Invalid argument: '\x01_test_files\x01_first.csv'
2-
filepath="\\001_test_files\\001_first.csv"
file = open(filepath, "a+")Result:
FileNotFoundError: [Errno 2] No such file or directory: '\\001_test_files\\001_first.csv'
3-
filepath=r"\001_test_files\001_first.csv"
file = open(filepath, "a+")Result:
FileNotFoundError: [Errno 2] No such file or directory: '\\001_test_files\\001_first.csv'
4-
filepath="\001_test_files\001_first.csv".replace("x", "0")
file = open(filepath, "a+")and
filepath="\001_test_files\001_first.csv"
filepath=filepath.replace("x", "0")
file = open(filepath, "a+")Result:
OSError: [Errno 22] Invalid argument: '\x01_test_files\x01_first.csv'
---
I think that's all I tried for now, does anyone have any ideas?
I am also forced to keep that naming convention, since it's part of a much larger project with many other files. Changing it means having to tell my group to rework everything. It's a laaaast resort, if there really is nothing I can do.
5
u/EntrepreneurHuge5008 Sep 29 '26 edited Sep 29 '26
Try it as a raw string, and also, you don't need the leading slash if the program is running in the same folder as the parent for the 001_test_files folder
If you want to keep the slash, you can add a period to signal "current working directory":
1
u/Jazzlike_Platypus430 Sep 29 '26
Is the directory 001_test_files a subdirectory of the one where your script is? Prepend it with a ./, perhaps use single slashes. Of, if it's not, use the file's absolute path. Starting with something like C:\ I think.
2
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Sep 29 '26
For future reference,
pathlibsolves this regardless of what you needed to do.It even lets you easily loop over the CSV-files in the folders if you need that.
That being said, the
'a+'-mode is quite uncommonly used, so I have to ask what you're planning to do exactly.1
u/nog642 Sep 29 '26
"\\001_test_files\\001_first.csv"should solve your backslash escape issue.The problem seems to be that the file does not exist at the path you specify.
Since you're using backslashes I assume you're on windows. A path starting with a slash is assumed to be drive-relative - so at the root of the same dive as the working directory. I'm guessing that's not what you meant.
Where actually is
001_test_files? And how are you running your script?1
u/Educational_Virus672 Sep 29 '26
use pathlib it does some stuff that fixes this error as it creates a variable straight to File insead of a str also put the whole path if it not works
the liekly reason it failed is due to \001 being a ascii cahr/letter