Invalid argument error and python 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 the top heavy fraction. I got some of the fractions to work when I entered my own values ​​into the program, but I can't get the program to recognize the text file. I tried to put them in the same directory and put the full system path to the file, but nothing worked so far. right now i am just trying to get the contents of a file to print In the meantime, there is no need to know about it. ”

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)      

      

Then I will assign 2 values ​​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'

      

thanks for any help.

+4


source to share


7 replies


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:



  • When working with paths that contain a backslash, you either need to use two backslashes or use a form r''

    to prevent the interpretation of escape sequences. For example, 'C:\\Program Files\\...'

    or r'C:\Program Files\...'

    .
  • Your error is shown: \\Startmenu\\

    . It looks like there is no space between Start and Menu, even though the open line seems to have the correct path.

Note: what \topheavy.txt

in your path will probably also convert to <tab>opheavy.txt

. This is why there are no two backslashes in the trace.

+3


source


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

And is the startmenu different in these two lines? Try using a forward instead of a backslash. Python converts 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'

0


source


Replace each \

with \\

in the file path

0


source


You are using the "\" separator, which probably escapes somewhere (like this \ t towards the end). This is the Windows path separator, but also used as an escape string.

You can double the value "\" as "\". However, the easiest way is to pre-add r at the beginning to ignore.

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

      

Skip the recommendation for use / instead you are not using Unix, and there is no reason why Python cannot host Windows if you remember to make sure the "\" is also an escape. Using r 'at startup also allows you to copy / paste from a string to another program, or vice versa.

it would also not hurt to test in c: \ temp or similar to avoid problems where you may not have specified your path.

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

0


source


You must add another "/" in the last "/" path, for example:

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

Link

0


source


This error occurred while trying to read a large file in Python 3.5.4. To solve this problem, instead of reading the entire file into memory with .read()

, I read each line one by one:

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

      

0


source


As written in the Python documentation, an IOError exception occurs:

Raised when an I / O operation (such as a print statement, built-in open () function, or a file object method) does not work for an I / O-related reason, such as "file not found" or "full disk".

-1


source







All Articles