好的,所以我基本上是在编写一个创建文本文件的程序,除了我希望它们创建在与.py文件位于同一文件夹的文件夹中,可能吗?我该怎么做?使用python 3.3
要查找脚本所在的目录:
import os
path_to_script = os.path.dirname(os.path.abspath(__file__))
Run Code Online (Sandbox Code Playgroud)
然后你可以使用它作为你的文件的名称:
my_filename = os.path.join(path_to_script, "my_file.txt")
with open(my_filename, "w") as handle:
print("Hello world!", file=handle)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3136 次 |
| 最近记录: |