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