Oserror [Errno 22] Invalid Argument When Use Open() in Python

Python -How to solve OSError: [Errno 22] Invalid argument

Your issue is with backslashing characters like \T :

Try:

f = open(r'C:\\Users\Tanishq\Desktop\python tutorials\test.txt', 'r')

Python uses \ to denote special characters. Therefore, the string you provided does not actually truly represent the correct filepath, since Python will interpret \Tanishq\ differently than the raw string itself. This is we we place the r in front of it. This lets Python know that we do indeed want to use the raw string and to treat \ as a normal character.

OSError: [Errno 22] Invalid argument while opening a file

Use translate() rather than translate_file(), since the line is not a filename.

You also need to use ast.literal_eval() to parse it into a dictionary.

from deep_translator import GoogleTranslator
import ast

with open('./test.txt', 'w',encoding='utf-8') as output_file, open('./data.txt', 'r', encoding='utf-8') as reader:
for row in reader:
d = ast.literal_eval(row)
column1= GoogleTranslator(source='english', target='german').translate(d['body'])
output_text = str(column1)
output_file.write(output_text + '\n')

Python: OSError: [Errno 22] Invalid argument: wrong path (on Output seems that Python modify my path )

Your problem is most likely this line:

file_path = 'C:\Something\booh'

In a Python string literal, backslashes are used to introduce special characters. For example \n means a newline and \b means a backspace. To actually get a backslash, you have to type \\. A backslash followed by a character with no special meaning is left alone, so \S actually means \S (though relying on this is probably a bad idea).

You can either type your line like this

file_path = 'C:\\Something\\booh'

or use Pythons "raw string" syntax, which turns off the special meaning of backslashes, and type

file_path = r'C:\Something\booh'

Notice that when you do

s = '\\'

the string referred to by s actually contains a single backslash. For example, len(s) will be 1, and print(s) will print a single backslash.

OSError: [Errno 22] Invalid argument: - Changing backslash to forward slash not helping! (Windows)

The usual best practice to keep OS paths consistent across platforms in pythong is using the os module:

import os

path1 = "Desktop/" + "folder1/" + "folder2/"
with open(path1, "r") as file:
pass
# here, script is not consistent across OS,
# and can be difficult to format correctly for Windows

# instead, do:
path2 = os.path.join("Desktop", "folder1", "folder2")
with open(path2, "r") as file:
pass
# now, your script can find your Windows files,
# and the same script works for MacOS, Linux platforms

This helps keep consistency across platforms, so you can avoid meticulous string formatting

OSError: [Errno 22] Invalid argument in PyCharm

You can try File -> Settings -> Project: -> Project Structure -> Click on Timestamp (folder) -> Click on Mark as Sources -> Apply -> OK.

I hope it help you!



Related Topics



Leave a reply



Submit