Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python os.chdir()在mac中不工作_Python_Macos_Chdir - Fatal编程技术网

Python os.chdir()在mac中不工作

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

我正在编写一个python脚本来接受用户输入,创建目录并更改为新目录。然而,当我调用os.chdir()和os.getcwd()时,它并没有像预期的那样工作。(Mac OS-linux)

有没有与os.chdir()等价的版本

替换

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电脑上运行