Swapnil Saurav
3 min readJul 21, 2022

--

What is OSError?
OSError is the type of error in OSError : [errno22] invalid argument. OSError is an error class for the OS module. It is a built-in exception in python, which is raised. It is raised when the error occurs due to some system failure. I/O failures also give rise to OSErrors.

When the disk is full, or the file cannot be found, OSError is raised. The subclasses of OSError are BlockingIOError, ChildProcessError, ConnectionError, FileExistsError, FileNotFoundError, etc. OSError itself is derived from the EnvironmentError.

What is errorno22 invalid argument?
As the name suggests, invalid argument errors occur when an invalid argument is passed to a function. If a function was expecting an argument of a particular data type but instead received an argument of a different data type, it will throw an invalid argument error.

import tensorflow as tf
tf.reshape(1,2)

This code will raise invalid argument error. The tf.reshape() function was expecting a tensor as an argument. But instead, it received 1 and 2 as the argument.

‘OSError : [errno22] invalid argument’ while using read_csv()
Read_csv() is a function in pandas which is used to read a csv file in python. We can read a csv file by accessing it through a URL or even locally. While reading a csv file using read_csv, python can throw OSError : [errno22] invalid argument error.

Let us try to understand it with the help of an example. The below code has been executed in python shell to access local files. First, we shall import the pandas file to use read_csv()

import pandas as pd
file = read_csv(“C:\textfile.csv”)

The above line of code will raise the below error.

OSError: [Errno 22] Invalid argument: ‘C:\textfile.csv’
The reason behind the error is that python does not consider the backslash. Because of that, it showed oserror invalid argument. So what we have to do is that instead of a backslash, we have to replace it with a forwarding slash.

Correct method:
file = read_csv(“C:/textfile.csv”)

‘OSError : [errno22] invalid argument’ while using open()
We can get OSError : [errno22] invalid argument error while opening files with the open() function. The open() function in python is used for opening a file. It returns a file object. Thus, we can open the file in read, write, create or append mode.
Let us understand the error by taking an example. We shall try to open a .txt file in read mode using open(). The file would be returned as an object and saved in variable ‘f’.

f = open(“C:\textfile.txt”,”r”)

The code will throw the below error.

Traceback (most recent call last):
File “”, line 1, in
f = open(“C:\textfile.txt”,”r”)
OSError: [Errno 22] Invalid argument: ‘C:\textfile.

The OSError : [errno22] invalid argument error has been thrown because of the same reason as before. Here also, python fails to recognize the backslash symbol. On replacing backslash with forward slash, the error will be resolved.

Correct format:
f = open(“C:/textfile.txt”,”r”)

‘OSError : [errno22] invalid argument’ while reading image using open()
The above error can appear while opening an image using the open() function even though the backslash character has been replaced with forward slash. Let us see the error using an example.

image = open(“C:/image1.jpg”)

The error thrown would be:

Traceback (most recent call last):
File “”, line 1, in
image = open(“‪C:/image1.jpg”)
OSError: [Errno 22] Invalid argument: ‘\u202aC:/image1.jpg’

This error mainly occurs because of the copying of the file path. The Unicode characters also get copied sometimes when we copy the file path from our local system or the internet.

The Unicode character, ‘\u202a’ in the above example, is not visible in the file pathname. ‘\u202a’ is the Unicode control character from left to right embedding. So, it causes the above oserror invalid arguments.

The solution to this is straightforward. We simply have to type the URL manually instead of copying it. Thus, the Unicode character will no longer be in the URL and the error will be resolved.

What do you think? Please share in the comment section.

--

--

Swapnil Saurav

Swapnil Saurav has more than 18 years of experience in IT industry with focus on Supply Chain Analytics and IT Service Management.