Python os.chdir()在mac中不工作
我正在编写一个python脚本来接受用户输入,创建目录并更改为新目录。然而,当我调用os.chdir()和os.getcwd()时,它并没有像预期的那样工作。(Mac OS-linux) 有没有与os.chdir()等价的版本 替换Python os.chdir()在mac中不工作,python,macos,chdir,Python,Macos,Chdir,我正在编写一个python脚本来接受用户输入,创建目录并更改为新目录。然而,当我调用os.chdir()和os.getcwd()时,它并没有像预期的那样工作。(Mac OS-linux) 有没有与os.chdir()等价的版本 替换 os.chdir(cmdToExecute) 与: 您可以使用os.path.join连接路径: directoryPath = os.path.join(directoryPath, directoryName) 这在我的系统上运行良好: import os
os.chdir(cmdToExecute)
与:
您可以使用os.path.join
连接路径:
directoryPath = os.path.join(directoryPath, directoryName)
这在我的系统上运行良好:
import os
import sys
# Create a directory
directoryName = raw_input('Enter new directory name: ')
cmdToExecute = 'mkdir ' + directoryName
print cmdToExecute
os.popen(cmdToExecute)
# Change Directory
directoryPath = os.getenv('PWD')
directoryPath = os.path.join(directoryPath, directoryName)
os.chdir(directoryPath)
print os.getcwd()
输出:
Beagle:titanic_machine_learning kumarshubham$ python test.py
Enter new directory name: test
mkdir test
/Users/kumarshubham/titanic_machine_learning/test
替换
os.chdir(cmdToExecute)
与:
您可以使用os.path.join
连接路径:
directoryPath = os.path.join(directoryPath, directoryName)
这在我的系统上运行良好:
import os
import sys
# Create a directory
directoryName = raw_input('Enter new directory name: ')
cmdToExecute = 'mkdir ' + directoryName
print cmdToExecute
os.popen(cmdToExecute)
# Change Directory
directoryPath = os.getenv('PWD')
directoryPath = os.path.join(directoryPath, directoryName)
os.chdir(directoryPath)
print os.getcwd()
输出:
Beagle:titanic_machine_learning kumarshubham$ python test.py
Enter new directory name: test
mkdir test
/Users/kumarshubham/titanic_machine_learning/test
您没有更改为directoryPath,而是尝试将cmdToExecute作为一个侧注释,还有
os.mkdir
,如果您想避免调用外部mkdir
命令,您没有更改为directoryPath,而是尝试将cmdToExecute作为侧注释,还有os.mkdir
,如果您想避免调用外部mkdir
commandsorry。我正在使用os.chdir(directoryPath)您是否在windows或mac上运行这些命令。因为我只在Mac电脑上遇到问题,所以我在Mac电脑上运行对不起。我正在使用os.chdir(directoryPath)您是否在windows或mac上运行这些命令。因为我只在mac电脑上遇到问题,所以我在mac电脑上运行