Region: IOError: [Errno 22] invalid mode ('w') or filename
Use forward slashes:
'path/regionlog.txt'
Or raw strings:
r'path\regionlog.txt'
Or at least escape your backslashes:
'path\\regionlog.txt'
\r
is a carriage return.
Another option: use os.path.join
and you won't have to worry about slashes at all:
output = os.path.abspath(os.path.join('path', 'regionlog.txt'))
IOError: [Errno 22] invalid mode ('w') or filename
You cannot have slashes (/
) and colons (:
, but allowed in Unix) in your file name, but they are exactly what strftime
generates in its output.
Python tries to help you, it says:
No such file or directory: 'Bursarcodes_01/09/15_19:59:24.txt'
Replace time.strftime("%x")
with this:
time.strftime("%x").replace('/', '.')
...and time.strftime("%X")
with this:
time.strftime("%X").replace(':', '_')
IOError: [Errno 22] invalid mode ('r') or filename
You have an invisible character in your code. Use a hex editor or hex dumper to see it:
$ echo 'open("C:\\' | hd
00000000 6f 70 65 6e 28 22 e2 80 aa 43 3a 5c 5c 0a |open("...C:\\.|
The character in question is U+202a
, the left-to-right embedding character, encoded as UTF-8 as e2 80 aa
.
Remove the character from your source code by deleting ("C:
and retyping it.
IOError: [Errno 22] invalid mode ('wb') or filename:
You cannot use :
in Windows filenames, see Naming Files, Paths, and Namespaces ; it is one of the reserved characters:
The following reserved characters:
<
(less than)>
(greater than):
(colon)"
(double quote)/
(forward slash)\
(backslash)|
(vertical bar or pipe)?
(question mark)*
(asterisk)
Use a different character not on the reserved character list.
IOError: [Errno 22] invalid mode ('r') or filename: 'c:\\Python27\test.txt'
\t
is a tab character. Use a raw string instead:
test_file=open(r'c:\Python27\test.txt','r')
or double the slashes:
test_file=open('c:\\Python27\\test.txt','r')
or use forward slashes instead:
test_file=open('c:/Python27/test.txt','r')
IOError: [Errno 22] invalid mode ('rb') or filename: '\x89PNG\n'
The retrun value of askopenfilename
is a string. As you were traversing over a string (Probabily a path like C:\Somepath\someimage.png
) you got the following error.
Errno2: No such file or directory: u'C'
You need to use the askopenfilenames
module to get multiple files.
image_formats = [('PNG','*.png')]
file_path_list = askopenfilenames(filetypes=image_formats, initialdir='/', title='Please select a picture to analyze')
for file_path in file_path_list:
file_path = os.path.normpath(file_path) # To normalize path
image = data.imread(file_path)
image2 = data.imread(file_path)
Use for file_path in file_path_list.split()
for python<2.7.7, as the return value was a string instead of tuple.
https://docs.python.org/2/library/os.path.html#os.path.normpath
Orignal Question
Op had pasted code with askopenfile
instead of askopenfilename
tkFileDialog.askopenfile
returns selected file opened in r
mode if mode
is not specified.
def askopenfile(mode = "r", **options):
"Ask for a filename to open, and returned the opened file"
filename = Open(**options).show()
if filename:
return open(filename, mode)
return None
While the input to imread
is image file path.
You need to use askopenfilenames
which returns selected files path.
Scrapy IOError: [Errno 22] invalid mode ('wb') or filename
You failed to replace the special chars. Try this:
filename = os.path.join(directory,experience['Title'][0]+'.txt')
filename = filename.replace('\\' , "").replace('?' , "")
UPDATE
You just want to specify a legal file name. So I come up with an idea like this.
directory = os.path.join('Erowid/archive/',experience['Substance'][0].strip().lower())
filename = experience['Substance']+experience['Title'][0]+'.txt'
filename = "".join([i for i in filename if i in string.ascii_letters])
#only use ascii letters as file name
filename = os.path.join(directory, filename)
string.ascii_letters
The concatenation of the ascii_lowercase and ascii_uppercase constants described below. This value is not locale-dependent.
Related Topics
Panda's Dataframe - Renaming Multiple Identically Named Columns
Numpy.Where() Detailed, Step-By-Step Explanation/Examples
How to Change the Datetime Tick Label Frequency for Matplotlib Plots
Copy File or Directories Recursively in Python
Rename Nested Field in Spark Dataframe
How to Convert Defaultdict to Dict
How to Assign the Value of a Variable Using Eval in Python
Matplotlib Dateformatter for Axis Label Not Working
Smtplib Sends Blank Message If the Message Contain Certain Characters
Reset Color Cycle in Matplotlib
Python Integer Incrementing with ++
Python's Sum VS. Numpy's Numpy.Sum
Does Flask Support Regular Expressions in Its Url Routing
Python Saving Multiple Figures into One PDF File
How to Ssh Connect Through Python Paramiko with Ppk Public Key