在另一个目录中运行 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12790986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:52:56  来源:igfitidea点击:

Running a Python Script Inside Another Directory

python

提问by Daniel Scocco

I have the following Python script inside a directory called 'test' on my Linux desktop:

我的 Linux 桌面上名为“test”的目录中有以下 Python 脚本:

#!/usr/bin/python

f = open('test.txt','w')
f.write('testing the script')

So it's /Home/Desktop/test/script.py

所以是 /Home/Desktop/test/script.py

If I go inside the directory and type ./script.pyit works fine and creates the test.txt file.

如果我进入目录并输入./script.py它,它工作正常并创建 test.txt 文件。

However for some reason I am not able to run the script from the Desktop (/Home/Desktop). I tried ./test/script.py, for instance, but didn't work.

但是,由于某种原因,我无法从桌面 ( /Home/Desktop)运行脚本。./test/script.py例如,我尝试过,但没有奏效。

The file permissions on the script are 755, and on the directory 777.

脚本的文件权限是755, 和 目录777

Any help would be appreciated.

任何帮助,将不胜感激。

采纳答案by Tadeck

You can use os.path.dirname()and __file__to get absolute paths like this:

您可以使用os.path.dirname()__file__获取这样的绝对路径:

#!/usr/bin/python

import os  # We need this module

# Get path of the current dir, then use it to create paths:
CURRENT_DIR = os.path.dirname(__file__)
file_path = os.path.join(CURRENT_DIR, 'test.txt')

# Then work using the absolute paths:
f = open(file_path,'w')
f.write('testing the script')

This way the script will work on files placed only in the same directoryas the script, regardless of the place from which you execute it.

这样,脚本将只处理与脚本位于同一目录中的文件,而不管您从何处执行它。

回答by tink

"And so on" doesn't meant much.

“等等”没有多大意义。

Where in the file-system are you? What is the test directories relative position to your location?

你在文件系统的哪个位置?测试目录相对于您所在位置的位置是什么?

Have you tried a fully qualified path? E.g.,

您是否尝试过完全限定的路径?例如,

/home/daniel/test/script.py

回答by alvonellos

In your open('test.txt', 'w')put open(r'./test.txt', 'w'). When you run it, use "python script.py. See if that works.

在你open('test.txt', 'w')open(r'./test.txt', 'w'). 当你运行它时,使用 "python script.py。看看它是否有效。

回答by dckrooney

What directory are you executing in? You might try using:

你在哪个目录下执行?您可以尝试使用:

import os

print os.getcwd()

to verify that the working directory is what you think it is.

验证工作目录是否是您认为的那样。

回答by ernie

If your cwd is /Desktop/test, and then you try to execute ./test/script.py, you're trying to run a script at /Desktop/test/test/script.py. More likely, you just wanted to do ./script.py.

如果您的 cwd 是/Desktop/test,然后您尝试执行./test/script.py,则您正在尝试在/Desktop/test/test/script.py. 更有可能的是,您只是想做./script.py.

As an aside, your question would have been more useful if you had provided the error message you got from the command line, rather than just saying "didn't work"

顺便说一句,如果您提供了从命令行获得的错误消息,而不是仅仅说“不起作用”,那么您的问题会更有用

If the script is running and nothing is echoed to the console, most likely it's working. Note that opening a file in 'w' mode truncates the file. Maybe you want to use a+?

如果脚本正在运行并且没有任何内容回显到控制台,则很可能它正在运行。请注意,以“w”模式打开文件会截断文件。也许你想用一个+?