如何在其他目录中执行python脚本? [英] How to execute a python script in a different directory?
问题描述
已解决,请参见下面我的答案,以寻求可能对您有帮助的任何人.
Solved see my answer below for anyone who might find this helpful.
我有两个脚本a.py和b.py.在我当前的目录"C:\ Users \ MyName \ Desktop \ MAIN"中,运行> python a.py.
I have two scripts a.py and b.py. In my current directory "C:\Users\MyName\Desktop\MAIN", I run > python a.py.
第一个脚本a.py在我的当前目录中运行,对一堆文件进行处理,并创建一个新目录(testA),并将这些文件的编辑版本同时移到该新目录中.然后我需要为testA中的文件运行b.py.
The first script, a.py runs in my current directory, does something to a bunch of files and creates a new directory (testA) with the edited versions of those files which are simultaneously moved into that new directory. Then I need to run b.py for the files in testA.
作为初学者,我只需将b.py脚本复制并粘贴到testA中,然后再次执行命令> python b.py",它将在这些新文件上运行一些命令,并使用这些文件创建另一个文件夹(testB)编辑的文件.
As a beginner, I would just copy and paste my b.py script into testA and execute the command again "> python b.py", which runs some commands on those new files and creates another folder (testB) with those edited files.
我试图消除等待a.py完成,进入新目录,粘贴b.py,然后运行b.py的麻烦.我试图编写一个bash脚本来执行这些脚本,同时保持目录的层次结构.
I am trying to eliminate the hassle of waiting for a.py to finish, move into that new directory, paste b.py, and then run b.py. I am trying to write a bash script that executes these scripts while maintaining my hierarchy of directories.
#!/usr/bin/env bash
python a.py && python b.py
脚本a.py运行平稳,但是b.py根本不执行.没有有关b.py失败的错误消息,我只是认为它无法执行,因为一旦a.py完成,b.py在该新目录中就不存在.我可以在b.py中添加一个小脚本,将其移动到新目录中吗?我实际上也尝试过更改b.py目录路径,但是没有用.
Script a.py runs smoothly, but b.py does not execute at all. There are no error messages coming up about b.py failing, I just think it cannot execute because once a.py is done, b.py does not exist in that NEW directory. Is there a small script I can add within b.py that moves it into the new directory? I actually tried changing b.py directory paths as well but it did not work.
例如在b.py中:
mydir = os.getcwd() # would be the same path as a.py
mydir_new = os.chdir(mydir+"\\testA")
我在b.py中的所有实例中都将mydirs更改为mydir_new,但这也没有什么不同...我也不知道如何将脚本移动到bash中的新目录中.
I changed mydirs to mydir_new in all instances within b.py, but that also made no difference...I also don't know how to move a script into a new directory within bash.
作为文件夹的流程图:
MAIN # main folder with unedited files and both a.py and b.py scripts
|
| (execute a.py)
|
--------testA # first folder created with first edits of files
|
| (execute b.py)
|
--------------testB # final folder created with final edits of files
TLDR:如果b.py依赖于testA中创建和存储的文件,那么我如何从主测试文件夹中执行a.py和b.py(bash脚本样式?).通常,我将b.py复制并粘贴到testA中,然后运行b.py-但是现在我有200多个文件,因此复制和粘贴是浪费时间.
TLDR: How do I execute a.py and b.py from the main test folder (bash script style?), if b.py relies on files created and stored in testA. Normally I copy and paste b.py into testA, then run b.py - but now I have 200+ files so copying and pasting is a waste of time.
推荐答案
最简单的答案可能是更改工作目录,然后从以下位置调用第二个 .py
文件:
python a.py && cd testA && python ../b.py
当然,您可能会发现编写一个为您完成所有任务的脚本甚至更加容易,例如:
将其另存为与 a.py
相同的目录中的 runTests.sh
是:
#!/bin/sh
python a.py
cd testA
python ../b.py
使其可执行:
chmod +x ./runTests.sh
然后,您只需输入目录并运行它即可:
Then you can simply enter your directory and run it:
./runTests.sh
这篇关于如何在其他目录中执行python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!