我使用linux系统。你知道吗
我有许多不同的文件夹,每个文件夹都有一个bash文件(bash文件在每个文件夹中是相等的)。这个bash文件运行简单的命令,例如加载环境、创建文件和文件夹、运行二进制应用程序(例如code1)
在这些文件夹中有一个python文件,我想在其中运行 基本文件夹/我的Python.py “文件夹1/myBash” “文件夹2/myBash” . . . “文件夹/myBash”
问题:当我运行python脚本(例如code2)时,bash文件不是在文件夹中执行的,它是在bash文件位于baseFoldes中时执行的,因此它在baseFolder中创建文件夹、文件等。 我不明白为什么。你知道吗
我使用了操作系统和子流程包:
os.system('shell command')
subprocess.run('shell command')
subprocess.call('shell command')
代码1
#!/bin/bash
mkdir myNewFolder
touch myNewFile
代码2
#!/usr/bin/env python3
import os
import subprocess
... other code ...
subprocess.run(fullPathFolder+"/myBash")
或者
subprocess.call(fullPathFolder+"/myBash")
或者
os.system(fullPathFolder+"/myBash")
baseFolder/myPython.py
" myNewFolder <<<<<<<<<<?????
" myNewFile <<<<<<<<<<?????
" folder1/myBash
" folder2/myBash
.
.
.
" folderN/myBash
bash在启动python脚本的文件夹中执行。您需要在之前使用
os.chdir()
更改路径。或者更好:将目标目录作为参数传递给shell脚本,并在文件名前面加上路径。你知道吗相关问题 更多 >
编程相关推荐