【发布时间】:2015-12-24 11:03:46
【问题描述】:
我在 tes.py 文件中有以下代码。
#!/usr/bin/python
import os
path = '/tmp'
f_path = os.chdir( path )
f = os.getcwd()
print f
os.system('cd f')
我想在执行 tes.py 文件时进入 /tmp 目录。但我收到以下错误。
[rishb@xxxxxxx ~]$ ./tes.py
/tmp
sh: line 0: cd: f: No such file or directory
有什么方法可以在 python 中实现我的目标?
【问题讨论】:
-
错误清楚地表明
f没有找到。检查/tmp中是否有f -
我想你的意思是:
os.system('cd '+f)? -
os.chdir()怎么样? -
您在最后一行使用了文字 'f' 而不是变量 f。试试@Leo 所说的。
-
f是 Python 中的一个变量,所以如果你只是把它放在一个字符串中,肯定它不起作用。字符串永远是字符串是字符串是字符串。
标签: python