This seems like it should be very easy:
f = open('C:\Users\john\Desktop\text.txt', 'r')
But I am getting this error:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
f = open('C:\Users\john\Desktop\text.txt', 'r')
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\robejohn\\Desktop\text.txt'
Any thoughts?
Your file name has backslash characters in it. Backslash is the escape character in Python strings. Either replace them with '/' characters or use r'C:\Users\john\Desktop\text.txt'.
You might also find the functions in os.path useful.
In Windows, paths use backslash. But if a string that must represent a path contains characters such as '\r' , '\t' , '\n' .... etc there will be this kind of problem. This is the precise reason why your string fails to represent a path.
In the absence of these problematic characters, there will be no problem. If they are present, you must escape the backslashes or use a raw string r'C:\Users\john\Desktop\text.txt'
Related
all. I'm running into a "no such file or directory" issue in python that's stumped me.
Things I've tried so far:
Closing any program that I could think might have the file open
Having the file in the same directory as the program I'm running
Using the absolute path name
Escaping the backslashes
Escaping the backslashes and spaces
Changing the backslashes to forward slashes
Removing all spaces, special symbols, and numbers from the filename
I even checked with os.getcwd and os.path.abspath and copy-pasted the path exactly.
I'm not sure what's going on here. I'm at a loss now. Would I get this same error if the file is still open in some elusive background program?
This is the relevant bit of code:
print(os.getcwd())
print(os.path.abspath('RainyGenki.json'))
deckName = "C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\RainyGenki.json"
deck = open(deckName, 'r') #opens card deck
This is the error message:
C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag
C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\RainyGenki.json
Traceback (most recent call last):
File "C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\kanji_drag\kanji_main.py", line 79, in <module>
deck = open(deckName, 'r') #opens card deck
IOError: [Errno 2] No such file or directory: 'C:\\Users\\myName\\My Documents\\LiClipse Workspace\\KanjiDrag\\RainyGenki.json'
Using a raw string...
If you are sure that your path is OK then use the python syntax for a raw string:
In plain English: String literals can be enclosed in matching single
quotes (') or double quotes ("). They can also be enclosed in matching
groups of three single or double quotes (these are generally referred
to as triple-quoted strings). The backslash () character is used to
escape characters that otherwise have a special meaning, such as
newline, backslash itself, or the quote character. String literals may
optionally be prefixed with a letter 'r' or 'R'; such strings are
called raw strings and use different rules for interpreting backslash
escape sequences. A prefix of 'u' or 'U' makes the string a Unicode
string. Unicode strings use the Unicode character set as defined by
the Unicode Consortium and ISO 10646. Some additional escape
sequences, described below, are available in Unicode strings. A prefix
of 'b' or 'B' is ignored in Python 2; it indicates that the literal
should become a bytes literal in Python 3 (e.g. when code is
automatically converted with 2to3). A 'u' or 'b' prefix may be
followed by an 'r' prefix.
This basically means that to escape the backslash escape sequences, you just need to put an 'r'before the string like:
deckName = r"C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\RainyGenki.json"
ck = open(deckName, "r")
And even though you say you have tried it, escaping the backslashes should also work:
deckName = "C:\\Users\\myName\\My Documents\\LiClipse Workspace\\KanjiDrag\\RainyGenki.json"
ck = open(deckName, "r")
I had a folder called KanjiDrag, and within that I had the actual source folder kanji_drag which is where the json file and the main module were. The path I was using was accessing the KanjiDrag folder, but not the kanji_drag folder, and I didn't catch the different names. This is my dumbest file IO mistake yet. Thanks for all the replies, many of which I'll still refer to later when I refine this part of my program.
I have a path to a file containing $ signs, which I escaped with \ so open() can handle the path. But now open turns the \$ to a \\$ automatically.
For example:
open("/home/test/\$somedir\$/file.txt", "r")
result in an error message
IOError: [Errno 2] No such file or directory: '/home/test/\\$somedir\\$/file.txt'
Can I supress this. Why open() do that? I can't find anything in the docu of open, which describes this.
open() doesn't do that. It's Python, which escapes any special characters when representing a string:
>>> path = '\$'
>>> path
'\\$'
>>> print path
\$
In a regular Python string literal, a \ has special meaning, so it is escaped when echoing back the value, which can be pasted right back into a Python script or interpreter session to recreate the same value.
On Linux or Mac, you generally do not need to escape a $ value in a filename; a $ has no special meaning in a regular python string, nor in most Linux or Mac filenames:
>>> os.listdir('/tmp/$somedir$')
['test']
>>> open('/tmp/$somedir$/test')
<open file '/tmp/$somedir$/test', mode 'r' at 0x105579390>
Try using a literal representation of the string adding r before the string variable to avoid dealing with more complex scape situations, for example:
print('C:\test')
#C: est
print(r'C:\test')
#C:\test
where \t is interpreted as a tab.
Using pygame (from pygame import *) I tried to load a picture, but this happened:
Traceback (most recent call last):
File "C:/Users/Ben/Documents/Python Files/Rocket game with things", line 15, in <module>
right_fin = image.load('C:\Users\Ben\Pictures\right.png').convert()
error: Couldn't open C:\Users\Ben\Picturesight.png
There was basicly no code before this, so im not going to post it. I haven't encounted this before, nor have any idea what the problem even is. Sorry if the answer's obvious.
Backslashes have special meaning in Python strings, and \r is the escape code for a carriage return character.
Use double slashes, forward slashes or r'' raw strings (which do not interpret backslashes as escape sequences) to define the path:
right_fin = image.load('C:\\Users\\Ben\\Pictures\\right.png').convert()
right_fin = image.load('C:/Users/Ben/Pictures/right.png').convert()
right_fin = image.load(r'C:\Users\Ben\Pictures\right.png').convert()
I'm trying to get os.walk() to work in a program I'm working on, but I keep getting the error:
ValueError: invalid \x escape
From looking around online, I've seen that the error can arise from not using a raw string.
However, I still keep getting the error...
import os
path = r'D:\Data\Tracking\'
for root, dirs, files in os.walk(path):
print root
print dirs
print files
Anyone have an idea of what I can do differently to make it work?
Try using \\ to prevent the last backslash from escaping the quote after it.
>>> path = r'D:\Data\Tracking\'
File "<input>", line 1
path = r'D:\Data\Tracking\'
^
SyntaxError: EOL while scanning string literal
>>> path = r'D:\Data\Tracking\\'
>>> print(path)
D:\Data\Tracking\\
You can do this without a raw string to get the exact string that you want:
>>> path = 'D:\\Data\Tracking\\'
>>> print(path)
D:\Data\Tracking\
I'm a little surprised that you're getting a ValueError... but notice that the problem is with the trailing '.
>>> path = r'D:\Data\Tracking'
>>> path = r'D:\Data\Tracking\'
File "<stdin>", line 1
path = r'D:\Data\Tracking\'
^
SyntaxError: EOL while scanning string literal
For workarounds, see Why can't a raw string end in an odd number of trailing backslashes
My favorite is:
>>> path = r'D:\Data\Tracking' '\\'
which uses automagic string concatenation of literals.
I an running this code:
#!/usr/bin/python coding=utf8
# test.py = to demo fault
def loadFile(path):
f = open(path,'r')
text = f.read()
return text
if __name__ == '__main__':
path = 'D:\work\Kindle\srcs\test1.html'
document = loadFile(path)
print len(document)
It gives me a trackback
D:\work\Kindle\Tests>python.exe test.py
Traceback (most recent call last):
File "test.py", line 11, in <module>
document = loadFile(path)
File "test.py", line 5, in loadFile
f = open(path,'r')
IOError: [Errno 22] invalid mode ('r') or filename: 'D:\\work\\Kindle\\srcs\test1.html'
D:\work\Kindle\Tests>
If I change the path line to
path = 'D:\work\Kindle\srcs\\test1.html'
(note the double \\) it all works fine.
Why? Either the separator is '\' or it is not, not a mix?
System. Windows 7, 64bit,
Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
Checked - and all the backslashes appear correctly.
The backslash is an escape character when the next character combination would result in a special meaning. Take the following examples:
>>> '\r'
'\r'
>>> '\n'
'\n'
>>> '\b'
'\x08'
>>> '\c'
'\\c'
>>>
r, n, and b all have special meanings when preceded by a backslash. The same is true for t, which would produce a tab. You either need to A. Double all your backslashes, for consistency, because '\\' will produce a backslash, or, B, use raw strings: r'c:\path\to\my\file.txt'. The preceding r will prompt the interpreter not to evaluate back slashes as escape sequences, preventing the \t from appearing as a tab.
You need to escape backslashes in paths with an extra backslash... like you've done for '\\test1.html'.
'\t' is the escape sequence for a tab character.
'D:\work\Kindle\srcs\test1.html is essentially 'D:\work\Kindle\srcs est1.html'.
You could also use raw literals, r'\test1.html' expands to:
'\\test1.html'
Use raw strings for Windows paths:
path = r'D:\work\Kindle\srcs\test1.html'
Otherwise the \t piece of your string will be interpreted as a Tab character.
The backslash \ is an escape character in Python. So your actual filepath is going to be D:\work\Kindle\srcs<tab>est1.html. Use os.sep, escape the backslashes with \\ or use a raw string by having r'some text'.
In addition to using a raw string (prefix string with the r character), the os.path module may be helpful to automatically provide OS-correct slashes when building a pathname.
Gotcha — backslashes in Windows filenames provides an interesting overview.