“无效参数”错误和python不读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26662247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:50:22  来源:igfitidea点击:

'invalid argument' error and python not reading file

pythonfile

提问by Sawyer

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.

我正在尝试编写在文本文件中包含 2 个数字然后将它们分开的代码,将答案显示为最重的分数。当我在程序中输入我自己的值时,我已经让分数部分工作,但我无法让程序识别文本文件。我尝试将它们放在同一目录中并放置文件的完整系统路径,但到目前为止没有任何效果。现在我只是想打印文件的内容。

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:

然后我将这 2 个值分配给 x 和 y,但我收到此错误:

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.

谢谢你的帮助。

回答by Vincent Beltman

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

正如 Python 文档中所写的那样,会发生 IOError 异常:

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”.

当 I/O 操作(例如打印语句、内置 open() 函数或文件对象的方法)因 I/O 相关原因失败时引发,例如“找不到文件”或“磁盘满的”。

回答by Vincent Beltman

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

用“r”(读)而不是“w”(写)打开

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

和这两行中的startmenu不一样??尝试使用正斜杠而不是反斜杠。Python 会将正斜杠转换为其运行的操作系统的适当分隔符

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

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'

OSError: [Errno 22] 无效参数:'C:\ProgramData\Microsoft\Windows\Startmenu\Programs\Python 3.4\topheavy.txt'

回答by Irshad Bhat

Replace every \with \\in file path

每更换\\\的文件路径

回答by John Szakmeister

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'
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.
  1. 处理包含反斜杠的路径时,您要么需要使用两个反斜杠,要么使用r''表格来防止转义序列的解释。例如,'C:\\Program Files\\...'r'C:\Program Files\...'
  2. 您的错误显示:\\Startmenu\\。尽管空行似乎有正确的路径,但“开始”和“菜单”之间似乎缺少一个空格。

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

注:\topheavy.txt在你的路径可能是越来越转化为<tab>opheavy.txt过。这就是为什么在回溯中它前面没有两个反斜杠的原因。

回答by JL Peyret

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.

您正在使用“\”分隔符,它可能会在某处被转义(就像结尾处的 \t 一样。这是 Windows 路径分隔符,但也用作字符串转义符。

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

您可以将“\”加倍为“\”。然而,最简单的方法是在开头添加 r 以忽略 .

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.

跳过使用 / 的建议,您不在 Unix 上,并且 Python 没有理由不能适应 Windows,只要您记得注意“\”也是一个转义符。在开始时使用 r' 还允许您从字符串复制/粘贴到另一个程序中,反之亦然。

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

此外,在 c:\temp 或类似内容中进行测试也无妨,以避免出现您可能输入错误路径的问题。

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

最后但并非最不重要的是,您需要以“r”读取模式打开,如前所述。

回答by Hiep Tran

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

您应该在路径的最后一个“/”中再添加一个“/”,例如:

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

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

Reference

参考

回答by duhaime

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:

尝试在 Python 3.5.4 中读取大文件时出现同样的错误。为了解决它,我没有将整个文件读入内存.read(),而是逐行读取:

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