Nat*_*055 26 python python-idle
所以,我正在学习Python,并希望创建一个简单的脚本来从Internet下载文件,然后将其写入文件.但是,我使用IDLE并且不知道IDLE中的工作目录是什么或者如何更改它.如果我不知道工作目录或如何更改它,如何在IDLE中执行文件系统操作?
pok*_*oke 33
您可以使用os.getcwd以下方法轻松检查自己:
>>> import os
>>> os.getcwd()
'C:\\Program Files\\Python33'
Run Code Online (Sandbox Code Playgroud)
那是在我的Windows机器上,所以它可能是Python本身的安装目录.
您可以在运行时使用os.chdir以下命令更改该目录
>>> os.chdir('C:\\Users\\poke\\Desktop\\')
>>> os.getcwd()
'C:\\Users\\poke\\Desktop'
>>> with open('someFile.txt', 'w+') as f:
f.write('This should be at C:\\Users\\poke\\Desktop\\someFile.txt now.')
Run Code Online (Sandbox Code Playgroud)
这并不奇怪 - 在我的桌面上创建文件.
您可以使用os.getcwd()以下方法检查:
In [1]: import os
In [2]: os.getcwd()
Out[2]: '/home/monty'
In [7]: os.chdir("codechef") #change current working directory
In [8]: os.getcwd()
Out[8]: '/home/monty/codechef'
Run Code Online (Sandbox Code Playgroud)
os.chdir():
In [4]: os.chdir?
Type: builtin_function_or_method
String Form:<built-in function chdir>
Docstring:
chdir(path)
Run Code Online (Sandbox Code Playgroud)
os.getcwd():
Change the current working directory to the specified path.
In [5]: os.getcwd?
Type: builtin_function_or_method
String Form:<built-in function getcwd>
Docstring:
getcwd() -> path
Return a string representing the current working directory.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46656 次 |
| 最近记录: |