English 中文(简体)
• 如何促使用户在Sharma开立档案
原标题:How to prompt a user to open a file in Python
  • 时间:2011-10-21 07:15:57
  •  标签:
  • python
  • file

我将以下代码用于根据用户确定的道路开立档案,但正出现错误。 任何建议?

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

它说,尽管我正在界定,但还是没有确定或不存在这样的东西。 查阅文件。

最佳回答

页: 1

myfile = open(f,  r )

<代码> f/code>系指由字母 f组成的示意图,因此,您的代码正在寻找一个称为<代码>f的文档,而不是找到。 而是使用<代码>f,这意味着变量 f的价值。

另外,没有把变数称作存储你的文档file。 这样做容易,但试图避免。 已经有了一个叫做file的现成班级,其最佳做法是,不以你自己的姓名隐藏任何已建班或职能。 这是因为你看到的其他代码将预期<代码>>>>>><>代码>代表档案类别而不是您的变量。

采用<代码>求助<>/代码>功能的一种方法:

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

而由于登革热在斯图尔德建议,在登上此刻的法典时,你完全有理由 in。

问题回答

你试图打开这块地块。 为此:

file = open(f,  r )

Don t putf in quotes. <代码>f是一种可变数,可加以扼制,但在你开放时,你正在使用直观价值 f。

file = open(f,  r )

公开(姓名、方式)归还档案标,最常见的是用两种论点:公开(档案名称、方式)。

>>> f = open(/tmp/workfile , w >/code>

http://docs.python.org/tutorial/inputoutput.html http://www.un.org。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...