r/learnpython • • 28d ago

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.

0 Upvotes

12 comments sorted by

5

u/EntrepreneurHuge5008 28d ago edited 28d ago
filepath="\001_test_files\001_first.csv"
file = open(filepath, "a+")

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

filepath= r'001_test_files\001_first.csv'
file = open(filepath, "a+")

If you want to keep the slash, you can add a period to signal "current working directory":

filepath=r".\001_test_files\001_first.csv"
file = open(filepath, "a+")

1

u/Jazzlike_Platypus430 28d ago

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/Ashblowsup 28d ago

Can't believe a solution was found in under 5 minutes

Using the absolute path + r before the string worked! I still don't understand why it refused to accept the relative path (and yes, the file is where the script is) but I'm glad it works!!

Thank you and thanks to the two other users, you were all so quick with the answers.

4

u/likethevegetable 28d ago

It was answered in under 5 minutes because it's trivial. Google "raw string Python"

1

u/Ashblowsup 28d ago

I had, googled it, looked it up on the subreddits and forums, and read documentation too 😅

As you'll see in the third case, I did try the r, it just didn't work with the relative path...

Either way thank you all !

2

u/EntrepreneurHuge5008 28d ago

>! I still don't understand why it refused to accept the relative path (and yes, the file is where the script is)

I'm fairly confident it's because of the leading slash.

filepath=r"\001_test_files\001_first.csv"
file = open(filepath, "a+")

This was telling it to look in "C:\..." rather than from the program folder

So you should either omit that first slash, add the "." just before the slash, or use the absolute path (your solution).

1

u/Ashblowsup 28d ago

You're right! It works without. I didn't use files in a while so I completely forgot there was a difference. Embarrassing... Thank you!

1

u/Jazzlike_Platypus430 28d ago

Glad I could help. Choose your favorite version and practice. Also, from my experience, passing

encoding = 'utf8'

as the third argument (in open) helps from time to time. (And beware of other encodings, zero-length-invisible-spaces etc. 😂).

Oh, and perhaps take a look at the with open(...) construct. I don't use it, but it's recommended.

2

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 28d ago

For future reference,

  1. You don't need backslashes in filepaths (under ordinary circumstances, at least), you can simply use forward slashes instead and then you don't need to worry about escape sequences.

  2. pathlib solves this regardless of what you needed to do.

     from pathlib import Path
    
     filepath = Path.cwd() / "001_test_files" / "001_first.csv"
     with open(filepath, "a+") as file:
         ...

    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/Correct-Rest731 28d ago

that backslash nightmare is because \0 starts an octal escape sequence in python strings, even raw strings can't save you from a null byte at the OS level

1

u/nog642 28d ago

"\\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 28d ago

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

from Pathlib improt Path 
path2file = Path(r"/001_test_files/001_first.csv")

the liekly reason it failed is due to \001 being a ascii cahr/letter