当我使用python创建文件时,文件在哪里
所以我开始创建一个python脚本来学习如何在python中创建和编写txt文件 以下是脚本:当我使用python创建文件时,文件在哪里,python,Python,所以我开始创建一个python脚本来学习如何在python中创建和编写txt文件 以下是脚本: f = open("my.abc", "x") f.write("Added more content") f.close() #open and read the file after the appending: f = open("my.abc", "r") print(f.read()) 当我第一次运行它时: Added more content 当我第二次运行它时: Added mor
f = open("my.abc", "x")
f.write("Added more content")
f.close()
#open and read the file after the appending:
f = open("my.abc", "r")
print(f.read())
当我第一次运行它时:
Added more content
当我第二次运行它时:
Added more contentAdded more content
但在我编写的脚本所在的目录中,没有同名文件
D:\python>dir
Volume in drive D is Python
Directory of D:\python
01/01/1970 00:00 PM <DIR> .
01/01/1970 00:00 PM <DIR> ..
01/01/1970 00:00 PM 157 test.py
1 File(s) xxx bytes
2 Dir(s) xxx bytes free
所以我想知道这些文件保存在哪里?您可以通过以下方式找到当前的工作目录:
import os
print(os.getcwd())
如果未指定路径,则应将文件保存在此处。文件将放在您告诉它们的位置。f=openmy.abc,x在my.abc打开文件,但该文件被解释为工作目录的相对路径。为了避免歧义,最好使用绝对路径。通常,除非代码中有cd,否则脚本所在的文件夹就是绝对路径。