0

I am trying to run the following code on ubuntu but getting file not found error. But same code is working fine on my windows machine

...
for elem in items['stats']:
        f = open(r"\root\bot_python\list.txt", "r+")
...
David
  • 23
  • 3

3 Answers3

1

You should use double back slash instead of single. Like this:

...
for elem in items['stats']:
        f = open(r"\\root\\bot_python\\list.txt", "r+")
... 
  • Double backslashes in a raw string are litteral double backslashes. Linux paths don't use backslashes at all. So no, you definitely don't want to use double backslashes here. – Thierry Lathuille Mar 29 '21 at 07:56
1

Because the path separator is different between Ubuntu and Windows. See Path separator for Windows and Unix and separator for both Linux and Windows. For python, you could use os.path.join to parse your file path before reading it. Btw, pathlib is also a good option if you're familiar with some object-oriented concepts.

t0ur1s4
  • 38
  • 4
-2

I think you need root permission to access the file see you are trying to access a file inside root directory... Try Running the python script with "sudo python file_name.py" Maybe it solves your issue. And use backward slashes for spaces whenever possible.