Python Forum
[Errno 22] Invalid argument - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [Errno 22] Invalid argument (/thread-5293.html)

Pages: 1 2


[Errno 22] Invalid argument - satyaneel - Sep-27-2017

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


RE: [Errno 22] Invalid argument - buran - Sep-27-2017

please, post your code in code tags as well as full traceback in error tags.


RE: [Errno 22] Invalid argument - satyaneel - Sep-27-2017

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'



RE: [Errno 22] Invalid argument - snippsat - Sep-27-2017

\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'



RE: [Errno 22] Invalid argument - buran - Sep-27-2017

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



RE: [Errno 22] Invalid argument - satyaneel - Sep-27-2017

wonderful ... It's working. Thanks a ton


RE: [Errno 22] Invalid argument - LOVESN - Feb-11-2020

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'


RE: [Errno 22] Invalid argument - snippsat - Feb-11-2020

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')



RE: [Errno 22] Invalid argument - LOVESN - Feb-11-2020

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!!


RE: [Errno 22] Invalid argument - tdandwa - May-18-2020

(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!