‘str’对象整数错误用于Python中的os.open

huangapple go评论58阅读模式
英文:

'str' object integer error for os.open in Python

问题

Here's the translated portion of your text:

"我是一个相对新手的程序员,正在尝试进行我的第一次文件读取。

我的目标是从一个从PDF复制的.txt文件中去除不必要的换行符,但我无法打开该文件。

我已经交换了斜杠的方向,因为在文件名字符串中,\被解释为异常字符。

我的问题是:

为什么这段代码:

import os
my_file = os.open('C:/Users/USER/Documents/20 Programming/Code institute 5 day coding challenge.txt', 'r')
my_file.close()

会产生这个错误?

TypeError: 'str' object cannot be interpreted as an integer

以下是完整的控制台输出:

"C:\Users\USER\Documents Programming\untitled\venv\venv\Scripts\python.exe" "C:\Users\USER\Documents Programming\untitled\remove carriage returns if no capital letter next.py" 
Traceback (most recent call last):
  File "C:\Users\USER\Documents Programming\untitled\remove carriage returns if no capital letter next.py", line 21, in <module>
    my_file = os.open('C:/Users/USER/Documents/20 Programming/Code institute 5 day coding challenge.txt', 'r')
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object cannot be interpreted as an integer

我已经尝试过谷歌搜索这个错误,但没有找到任何相关信息,因为这个错误与open()函数无关,就我所知。

我期望文件能够打开,以便我可以读取结果。

编辑:
感谢大家。我现在已经用open()替换了os.open()。现在我遇到了不同的错误(找不到文件),所以下一步我将尝试解决这个问题...(请注意,这不是一个新的问题!)"

英文:

I'm a fairly new programmer, and trying to do my first file read.

My aim is to strip unnecessary line breaks from text in a .txt file copied from a PDF, but I cannot get the file to open.

I already swapped the slash direction, as the \ for windows directories was interpreted as an exception character in the filename string

My question is:

Why does this code:

import os
my_file = os.open(&#39;C:/Users/USER/Documents/20 Programming/Code institute 5 day coding challenge.txt&#39;, &#39;r&#39;)
my_file.close()

give this error?

TypeError: &#39;str&#39; object cannot be interpreted as an integer

Here is the full console output:

&quot;C:\Users\USER\Documents Programming\untitled\venv\venv\Scripts\python.exe&quot; &quot;C:\Users\USER\Documents Programming\untitled\remove carriage returns if no capital letter next.py&quot; 
Traceback (most recent call last):
  File &quot;C:\Users\USER\Documents Programming\untitled\remove carriage returns if no capital letter next.py&quot;, line 21, in &lt;module&gt;
    my_file = os.open(&#39;C:/Users/USER/Documents/20 Programming/Code institute 5 day coding challenge.txt&#39;, &#39;r&#39;)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: &#39;str&#39; object cannot be interpreted as an integer

I've tried googling the error and had no joy, as this error isn't discussed in relation to open() as far as I can find.

I'm expecting the file to open so I can read the results

Edit:
Thanks everyone. I've now replaced os.open() with just open(). I've now got a different error (file not found) so I'll try to tackle that next... (note this is not a new question!)

答案1

得分: 2

正如其他人提到的,我不确定你为什么在使用 `open.os()`。至于你遇到的问题,你可以简单地使用:

    with open("file.txt", "r") as file:
        # 读取文件内容并删除所有换行符
        contents = file.read().replace('\n', '')

    # 打印文件内容,不包含换行符
    print(contents)

你得到错误的原因是因为 `open.os()` 期望的是一个整数常量作为第二个参数,而你传递了一个字符串 (`r`)。正确的代码应该类似于:

    import os
    my_file = os.open('C:/Users/USER/Documents/20 Programming/Code institute 5 day coding challenge.txt', os.O_RDONLY)
    os.close(my_file)
示例调用
`os.open(file, flags[, mode]);`

`os.O_RDONLY` 是指定只读打开文件的标志,它代表一个整数值。
你可以查阅任何函数的文档。例如,https://www.tutorialspoint.com/python/os_open.htm
英文:

As others have mentioned, I am not sure why you are using open.os(). As for the issue you are facing, you can simply use

with open(&quot;file.txt&quot;, &quot;r&quot;) as file:
    # read the contents of the file and remove all line breaks
    contents = file.read().replace(&#39;\n&#39;, &#39;&#39;)

# print the contents of the file without line breaks
print(contents)

The reason you are gettng the error is because open.os() expects an integer constant as the second argument where you are passing a string (r).The correct code would be something like

import os
my_file = os.open(&#39;C:/Users/USER/Documents/20 Programming/Code institute 5 day coding challenge.txt&#39;, os.O_RDONLY)
os.close(my_file)

example call
os.open(file, flags[, mode]);

os.O_RDONLY is the flag sayng open it for reading only it represents an integer value.
You can look up the docs for any function. For e.g https://www.tutorialspoint.com/python/os_open.htm

huangapple
  • 本文由 发表于 2023年5月7日 01:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定