【发布时间】:2020-05-20 23:21:04
【问题描述】:
我已经搜索和搜索,但没有找到并回答我的特定情况,或者如果我有我不知道如何实现它。我正在尝试使用 python 和 bash 创建一个脚本来自动化我的项目创建过程。而我会使用 1 个命令行参数运行 testing.py “因为我正在测试,所以现在调用它”,这将是项目文件夹的名称,然后它会询问“我想在哪里创建项目”路径的两个选项用于存储项目并使用 if 语句将路径存储到路径变量中。然后我想在 bash 脚本中传递该变量以导航到它并在那里创建项目目录。这是我目前所拥有的:
所以我要运行的命令是这样的:
python testing.py newProject
在.test.sh中
#!/bin/bash
function testing() {
cd
desktop
python testing.py $1
}
在 testing.py 中
import sys
import os
school = "/Users/albert/Open/Microverse/"
personal = "/Users/albert/Open/code/Projects/"
path = " "
userInput = input("What type of project would you like to create? Personal or School? ")
if userInput.lower() == "personal":
path = personal
elif userInput.lower() == "school":
path = school
def testing():
folderName = str(sys.argv[1])
os.makedirs(path + str(folderName))
print(folderName)
if __name__ == "__main__":
testing()
【问题讨论】:
-
这里出了什么问题?
-
当我运行 python 脚本时,它只会在桌面上创建项目文件夹,因为那是我存储 .test.sh 和 testing.py 文件的地方,但我不知道怎么做将路径变量从 python 传递给 bash 脚本函数的正确路径。
-
我知道路径变量正在获取正确的路径,因为当我在终端中仅使用函数中的 print(path) 运行它并打印路径变量时,它会输出正确的路径
标签: python bash automation scripting