如何在 Python 中提示用户打开文件

发布于 2024-12-11 04:21:41 字数 247 浏览 0 评论 0原文

我使用以下代码根据用户设置的路径打开文件,但出现错误。有什么建议吗?

f = raw_input("\n Hello, user. "
    "\n \n Please type in the path to your file and press 'Enter': ")
    file = open('f', 'r')

它说 f 未定义或不存在这样的东西......即使我正在定义它?使用“r”读取文件。

I'm using the following code to open a file based on the path set by the user, but am getting errors. Any suggestions?

f = raw_input("\n Hello, user. "
    "\n \n Please type in the path to your file and press 'Enter': ")
    file = open('f', 'r')

It says f is undefined or no such thing exists... even though I am defining it? Using 'r' to read the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一世旳自豪 2024-12-18 04:21:41

您不应将 f 放在引号中:

myfile = open(f, 'r')

'f' 表示由字母 f 组成的字符串,因此您的代码正在查找名为 f 的文件代码>但没有找到它。相反,使用 f 表示变量 f 的值。

另外,不要调用变量来存储文件file。这很容易做到,但要尽量避免。已经有一个名为 file 的内置类,最好不要用您自己的名称隐藏任何内置类或函数。这是因为您看到的其他代码将期望 file 代表文件类而不是您的变量。

查看术语是否正在使用的一种方法是使用 help 函数:

>>> help(file)

Help on class file in module __builtin__:

class file(object)
 |  file(name[, mode[, buffering]]) -> file object
 |  
 |  Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
 |  writing or appending.  The file will be created if it doesn't exist

由于缩进在 Python 中很重要,我建议在此处发布代码时确保缩进完全正确。

You shouldn't have the f in quotes:

myfile = open(f, 'r')

'f' means the string consisting of the letter f so your code was looking for a file called f and not finding it. Instead use f which means the value of the variable f.

Also, don't call the variable to store your file file. This is easily done but try to avoid it. There is already a built-in class called file and it's best practice not to hide any built-in classes or functions with your own names. This is because other code you see will expect file to represent the file class and not your variable.

One way to see if a term is in use is to use the help function:

>>> help(file)

Help on class file in module __builtin__:

class file(object)
 |  file(name[, mode[, buffering]]) -> file object
 |  
 |  Open a file.  The mode can be 'r', 'w' or 'a' for reading (default),
 |  writing or appending.  The file will be created if it doesn't exist

And as indendation is significant in Python I'd recommend getting your indentation exactly right when posting code on here.

π浅易 2024-12-18 04:21:41

您正在尝试打开字符串“f”。试试这个:

file = open(f, 'r')

You're trying to open the string 'f'. Try this:

file = open(f, 'r')
栀子花开つ 2024-12-18 04:21:41

不要将 f 放在引号中。 f 是一个保存字符串的变量,但在 open 中您使用的是字符串值“f”。

file = open(f, 'r')

Don't put f in quotes. f is a variable that is holding a string, but in your open you are using the string value 'f'.

file = open(f, 'r')
各自安好 2024-12-18 04:21:41

open() 返回一个文件对象,最常与两个参数一起使用:open(filename, mode)。

<代码>>> f = open('/tmp/workfile', 'w')

有关文件的更多信息,您可以查看此链接

open() returns a file object, and is most commonly used with two arguments: open(filename, mode).

>>> f = open('/tmp/workfile', 'w')

For more Information about Files U can Check out This Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文