Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Errno 22] Invalid argument
#1
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
Reply
#2
please, post your code in code tags as well as full traceback in error tags.
Reply
#3
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'


User has been warned for this post. Reason: No BBcode, not following instructions
Reply
#4
\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'
Reply
#5
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
Reply
#6
wonderful ... It's working. Thanks a ton
Reply
#7
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'
Reply
#8
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')
Reply
#9
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!!
Reply
#10
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Invalid argument: 'images\x08ackground.jpg' CatBall 4 902 Jun-19-2023, 09:28 AM
Last Post: CatBall
  [split] [Errno 22] Invalid argument Junaid 0 2,253 Jun-12-2021, 06:02 PM
Last Post: Junaid
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,738 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,728 Mar-03-2020, 08:34 AM
Last Post: buran
  Invalid argument error thrown. pyseeker 4 8,568 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  OSError: [Errno 22] Invalid argument - wasn't there last time I ran the code! meganhollie 2 9,124 Jun-11-2018, 06:01 PM
Last Post: meganhollie
  [SOLVED] OSError: [Errno 22] Invalid argument Panda 13 43,613 Jun-04-2018, 08:33 PM
Last Post: volcano63
  [Errno 22] Invalid argument Asafb 8 13,429 Dec-19-2017, 12:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020