开发者

How do you find the filename that you pass to open()?

I'm trying to open a file 开发者_Python百科with Python, but I'm unsure how to find the correct filename to use.


You can specify the path to the file in either a complete way (e.g. 'c:/wher/ever/the.txt'), also known as "absolute" because it's taken exactly as you specify it, or a partial one (e.g., just "the.txt", or "ever/the.txt", or "../ever/the.txt", and so on), also known as "relative" because it's taken relatively to the current working directory of your process. If you don't know that working directory, an absolute path is usually simplest to find and specify.

So, find out where the file lives (e.g. c:/wher/ever) and use that absolute path (with "rightside up slashes", instead of windows-style backslashes, as I just explained in another answer) to open the file in question.


Access the name attribute.

fh = open('spam.txt')
print fh.name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜