Python Forum

Full Version: [Errno 22] Invalid argument
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
When I am trying to open a html file with read mode in python spyder I am getting the following error
[Errno 22] Invalid argument
please, post your code in code tags as well as full traceback in error tags.
code:
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 20 07:44:11 2017

@author: Srinu
"""
f=open('E:\baby2008.txt','r')
Error:
Error:
OSError: [Errno 22] Invalid argument: 'E:\x08aby2008.txt'
\b is an escape characters,never use single\ in a path.
Turn around/,double up or raw string all work.
>>> s = '\baby'
>>> s
'\x08aby'

# Other way
>>> s = '/baby'
>>> s
'/baby'

# Double up
>>> s = '\\baby'
>>> s
'\\baby'

# Raw string
>>> s = r'\baby'
>>> s
'\\baby'
you are on Windows, that uses backslash as path separator \ However for python this is escape char, so you need to use forward slash or raw string or escape the backslash. In addition you should use with context manager when open the file. It will close the file for you at the end (something you don't do in your code)

with open('E:/baby2008.txt','r') as f:
# do something here
with open(r'E:\baby2008.txt','r') as f:
# do something here
with open('E:\\baby2008.txt','r') as f:
# do something here
wonderful ... It's working. Thanks a ton
Hi guys, I am having the same issue as the original poster. I have tried to use the suggested methods to deal with it and they don't seem to be working.

Here is my code:

#load in ENSO data
enso = pd.read_excel(r'‪C:\Users\Eli T\Desktop\ENSO.xlsx', 'r')
Error message:
OSError: [Errno 22] Invalid argument: '\u202aC:\\Users\\Eli T\\Desktop\\ENSO.xlsx'
Look like your editor have introduced a non-printing character U-202A is LEFT-TO-RIGHT EMBEDDING.
It's there even if you can not see it,this is a copy of code from your post.
>>> s = r'‪C:\Users\Eli T\Desktop\ENSO.xlsx'
>>> s
'\u202aC:\\Users\\Eli T\\Desktop\\ENSO.xlsx'
A temp fix could be this,you should investigate editor,eg try and other and see if it's happen there to.
>>> s = r'‪C:\Users\Eli T\Desktop\ENSO.xlsx'
>>> s
'\u202aC:\\Users\\Eli T\\Desktop\\ENSO.xlsx'
>>> 
>>> s = s.lstrip('\u202a')
>>> s
'C:\\Users\\Eli T\\Desktop\\ENSO.xlsx'
>>> enso = pd.read_excel(s, 'r')
That did work for the most part. When I get to the end where I try to read in the file though, it says:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Eli T\\Desktop\\ENSO.xlsx'

Edit: Actually this worked, I made an error on my end. Thanks!!
(Feb-11-2020, 03:24 PM)LOVESN Wrote: [ -> ]That did work for the most part. When I get to the end where I try to read in the file though, it says:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Eli T\\Desktop\\ENSO.xlsx'

Edit: Actually this worked, I made an error on my end. Thanks!!

that has worked!
Pages: 1 2