简体   繁体   English

Python:如何让python从其文件夹中打开文件?

[英]Python: How do I get python to open files out of it's folder?

I made a program to open files windows can't but I have to put the file everywhere I want to use it.我制作了一个程序来打开windows不能打开的文件,但我必须把文件放在我想使用它的任何地方。 How can I just give it a path to use to find the file to read?我怎样才能给它一个路径来查找要读取的文件?

Basically how would I open a file out of a program's folder?基本上我将如何从程序文件夹中打开文件? I would think that it would be... file = open('C://Users/Name/Python33/file','r') But that isn't working.我认为它会是... file = open('C://Users/Name/Python33/file','r')但这不起作用。 Could someone give me example code?有人可以给我示例代码吗?

Use command line arguments:使用命令行参数:

sys.argv[...] sys.argv[...]

You can do this either through command line arguments, as suggested by @qarma, or any other kind of I/O (taking raw_input , for instance).您可以通过@qarma 建议的命令行参数或任何其他类型的 I/O(例如,使用raw_input )来执行此操作。 If you're unfamiliar with accessing files by paths in Python, you may find os.path useful.如果您不熟悉在 Python 中通过路径访问文件,您可能会发现os.path很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM