[英]'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.我正在尝试编写在文本文件中包含 2 个数字然后将它们分开的代码,将答案显示为最重的分数。 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:然后我将这 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'
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: 两件事情:
r''
form to prevent interpreting of escape sequences. r''
形式来防止解释转义序列。 For example, 'C:\\\\Program Files\\\\...'
or r'C:\\Program Files\\...'
. 'C:\\\\Program Files\\\\...'
或r'C:\\Program Files\\...'
。 \\\\Startmenu\\\\
. \\\\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. 注意:您路径中的
\\topheavy.txt
也可能会转换为<tab>opheavy.txt
。 That's why there aren't two backslashes in front of it in the traceback. 这就是为什么在追溯中它前面没有两个反斜杠的原因。
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. 跳过使用/的建议,您不是在Unix上,并且没有理由Python无法容纳Windows,只要您记得注意“ \\”也是转义即可。 Using r' at start also allows you to copy/paste from the string into another program or vice-versa.
在开始处使用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”读取模式打开,如前所述。
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, eg, “file not found” or “disk full”.
当I / O操作(例如打印语句,内置open()函数或文件对象的方法)由于与I / O相关的原因而失败时引发,例如“找不到文件”或“磁盘”充分”。
Open with "r" (read) instead of "w" (write) 用“ r”(读)而不是“ w”(写)打开
And startmenu in these two lines are different?? 这两行的startmenu不同吗? 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
Python会将正斜杠转换为适合其运行的操作系统的分隔符
open('C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Python 3.4\\topheavy.txt','w') 打开('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'
在文件路径中用\\\\
替换每个\\
I had this same error appear when trying to read a large file in Python 3.5.4. 尝试在Python 3.5.4中读取大文件时出现了相同的错误。 To solve it, instead of reading the whole file into memory with
.read()
, I read each line one by one: 为了解决它,我没有用
.read()
将整个文件读入内存, .read()
读取每一行:
with open('big.txt') as f:
for i in f:
print(i)
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.我的问题,相当随意,是我正在使用
open(filename, "w").write(...)
filename
,其中filename
是无效的路径名或包含意外的斜杠。
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.例如,将
datetime.datetime.today()
转换为带有斜杠或冒号 (Windows) 的datetime.datetime.today()
字符串并写入不存在的目录将导致此错误。
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.举个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.