我想要建立一个程序来保存一些文档文件。我的程序在“测试”文件夹中运行,我希望在“测试\保存”中保存文档。我用这样的方式:
File=open("C:\\test\\save\\hello.txt",'w')
File.write("hello world")
File.close()现在我将我的程序复制到E驱动程序并运行程序,然后在C驱动程序中运行hello.txt make,而不是在E驱动器中。我应该做什么来保存文档在我的测试文件夹的每一个地方??
发布于 2021-01-22 20:18:03
你有程序写到那个绝对的文件路径。如果希望它在给定的目录中运行,只需使用类似于hello.txt的文件名即可。
发布于 2021-01-24 18:37:40
,我想你想要这样的东西:
from os.path import dirname, realpath, join
your_current_app_directory = dirname(realpath(__file__))
your_custom_folder = "test"
with open(join(your_current_app_directory, your_custom_folder, "hello.txt"), "w") as export:
export.write("hello")它将是动态的;取决于您的应用程序目录。
https://stackoverflow.com/questions/65852160
复制相似问题