最近写了个脚本,阔以一键创建目录,并且下载代码。
这里用到了python的os模块。该模块可以操作系统命令,并且适配windows和Linux。
os.system(‘mkdir hello’)
则可以创建一个hello的文件夹。
但是当程序运行到:
os.system(‘cd hello’)
时候,虽然执行了该条命令,但是当前系统并没有切换到hello的目录下。
我试着在python的命令行模式下操作:
>>os.system('pwd')
>>./opt/code
>>os.system('cd hello')
>>0
>>os.system('pwd')
>>./opt/code
可以看到,os.system(‘cd hello’)并没有改变当前的文件目录。
查找资料发现,原来在python中和os.system实现机制有关系。
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(),