【发布时间】:2018-02-07 15:35:21
【问题描述】:
我想导出一个 txt 文件,但它只能找到的目录是我的服务器的安装位置。
场景: 我的服务器在 Ubuntu 中,我在 Windows 浏览器中访问我的基于 Web 的系统,每当我生成一个 txt 文件时,它都会保存在 /opt 而不是我使用的 PC 的 /downloads 中。是否可以找到任何 PC 的活动目录? 或任何可能的解决方案?
我试过 os.chdir(C:\Windows\System32) 但它无法找到该路径
提前致谢。 [编辑]
a = random.randint(1, 9999)
name = 'debTest' + str(a * 7) + '.txt'
completeName = os.path.join('./Downloads', name)
print 'completname', completeName
file = open(completeName, 'w')
file.write("TEST FILE")
file = open(completeName, 'r')
file.close()
我正在尝试这段代码:
【问题讨论】:
-
你在写程序吗?如果是这样,请发布不适合您的代码。顺便说一句,当前工作目录可以在 Python 脚本中作为
os.getcwd()获得。任何平台都一样。 -
@PaulCornelius 这里是我刚才试过的代码,但还是不行:(我会编辑上面的帖子)
-
完成写入后立即关闭文件 - 通常文件在关闭之前将是空的。然后当你打开文件阅读它时,你写的文本就会在那里。您还应该在 Windows 资源管理器中看到该文件,并且它不应为空。
标签: python file path directory