Sawyer
Sawyer

Reputation: 97

'invalid argument' error opening file (and not reading file)

I am trying to write code that takes 2 numbers in a text file and then divides them, showing the answer as a top heavy fraction. I have gotten the fractions part to work when I am inputting my own values in the program, but i cannot get the program to recognise the text file. I have tried putting them in the same directory and putting the full system path of the file, but nothing so far has worked. right now I am just trying to get the contents of the file to print.

with open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w') as f:
    for line in f:
        for word in line.split():
            print(word)      

I will then assign the 2 values to x and y, but I get this error:

Traceback (most recent call last):
File "C:\Python34\divider.py", line 2, in <module>
open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')
OSError: [Errno 22] Invalid argument:'C:\\ProgramData\\Microsoft\\Windows\\Startmenu\\Programs\\Python 3.4\topheavy.txt'

Upvotes: 9

Views: 49758

Answers (9)

dgeorgiev
dgeorgiev

Reputation: 941

One can also bump into this on Windows when using linux emulators like Cygwin and creating "symlinks" that are then passed to python. Even if the path is valid, trying to open the symlink results in invalid argument error for me.

Upvotes: 0

Jack Hales
Jack Hales

Reputation: 1664

My issue, rather arbitrary, was I was writing a file using open(filename, "w").write(...), where filename is an invalid pathname or includes unexpected slashes.

For example, converting a datetime.datetime.today() into a datestring with slashes or colons (Windows) and writing to non-existing directories will result in this error.

Change:

open("../backup/2021/08/03 15:02:61.json", "w").write(data)

To:

open("../backup/2021-08-03 15-02-61.json", "w").write(backup)

As an example.

Upvotes: 1

duhaime
duhaime

Reputation: 27611

I had this same error appear when trying to read a large file in Python 3.5.4. To solve it, instead of reading the whole file into memory with .read(), I read each line one by one:

with open('big.txt') as f:
  for i in f:
    print(i)

Upvotes: 1

Hiep Tran
Hiep Tran

Reputation: 4093

You should add one more "/" in the last "/" of path for example:

open('C:\Python34\book.csv') to open('C:\Python34\\\book.csv')

Reference

Upvotes: 1

Irshad Bhat
Irshad Bhat

Reputation: 8709

Replace every \ with \\ in file path

Upvotes: 0

JL Peyret
JL Peyret

Reputation: 12214

You are using a "\" separator which is probably getting escaped somewhere (like that \t near the end. That's the Windows path separator, but also used as a string escape.

You can double up the "\" as "\". Easiest however is to prepend an r at the beginning to ignore .

r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt"

Skip the recommendation to use / instead, you are not on Unix and there is no reason Python can't accommodate Windows, as long as you remember to take care about "\" also being an escape. Using r' at start also allows you to copy/paste from the string into another program or vice-versa.

also, it wouldn't hurt to test in c:\temp or similar to avoid issues where you may have mistyped your path.

Last, but not least, you need to open in "r" read mode, as previously mentioned.

Upvotes: 1

John Szakmeister
John Szakmeister

Reputation: 47122

open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')
OSError: [Errno 22] Invalid argument:'C:\\ProgramData\\Microsoft\\Windows\\Startmenu\\Programs\\Python 3.4\topheavy.txt'

Two things:

  1. When working with paths that contain backslashes, you either need to use two backslashes, or use the r'' form to prevent interpreting of escape sequences. For example, 'C:\\Program Files\\...' or r'C:\Program Files\...'.
  2. Your error shows this: \\Startmenu\\. It appears that a space is missing between "Start" and "menu", despite the fact that the open line seems to have the right path.

Note: that the \topheavy.txt in your path is probably getting converted to <tab>opheavy.txt too. That's why there aren't two backslashes in front of it in the traceback.

Upvotes: 8

user4171906
user4171906

Reputation:

Open with "r" (read) instead of "w" (write)

And startmenu in these two lines are different?? Try using a forward instead of a back slash. Python will convert the forward slash to the appropriate delimiter for the OS it is running on

open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')

OSError: [Errno 22] Invalid argument:'C:\ProgramData\Microsoft\Windows\Startmenu\Programs\Python 3.4\topheavy.txt'

Upvotes: 0

Vincent Beltman
Vincent Beltman

Reputation: 2112

Just as is written on the Python Documentation, the IOError Exception occurs:

Raised when an I/O operation (such as a print statement, the built-in open() function or a method of a file object) fails for an I/O-related reason, e.g., “file not found” or “disk full”.

Upvotes: 0

Related Questions