如何在Python 3中的另一个文件夹中打开Python 3程序

时间:2016-10-21 12:23:48

标签: python

我想在Python 3中打开一个Python 3程序,但目标程序在另一个文件夹中。

EG:Pythonloader.py> PythonFolder> Target.py

我正在使用Python 3.3.0

2 个答案:

答案 0 :(得分:1)

使用子进程模块运行任何外部程序:

import subprocess
subprocess.call('python3 /path/to/target.py')

检查https://docs.python.org/2/library/subprocess.html以查看评论。

如果要导入要在脚本中使用的代码:

import sys
sys.path.append('/path/to/containing/directory')
import target

然后你可以使用其中的代码。

答案 1 :(得分:0)

您的代码运行正常。我能够猜出4个数字,它正确地停止了我。看起来你正确编写了程序,但你也无法猜测数字。

$ python foo.py
First >> 1
Second >> 2
Third >> 3
Fourth >> 4
*
First >> 5
Second >> 6
Third >> 7
Fourth >> 8

First >> 1
Second >> 1
Third >> 1
Fourth >> 1
**
First >> 2
Second >> 2
Third >> 2
Fourth >> 2
*
First >> 3
Second >> 3
Third >> 3
Fourth >> 3

First >> 4
Second >> 4
Third >> 4
Fourth >> 4
*
First >> 1
Second >> 1
Third >> 2
Fourth >> 4
**
First >> 1
Second >> 2
Third >> 1
Fourth >> 4
*
First >> 1
Second >> 1
Third >> 4
Fourth >> 2
*
First >> 4
Second >> 1
Third >> 1
Fourth >> 2

First >> 1
Second >> 4
Third >> 1
Fourth >> 2
**
First >> 1
Second >> 4
Third >> 2
Fourth >> 1
****