【发布时间】:2015-07-16 08:33:58
【问题描述】:
我有一个脚本,我在编写时只考虑了 Unix/Mac OS。
除其他外,它将文件存储在给定文件夹中
data_path = config.data_path
os.chdir(data_path)
testfile = urllib.URLopener()
try:
local_size = 0
remote_size = int(urllib.urlopen(remote_name).headers['Content-Length'])
if os.path.exists(data_path + temp_zip_name):
local_size = pathlib.Path(data_path + temp_zip_name).stat().st_size
if local_size != remote_size:
print 'downloading ' + temp_zip_name
testfile.retrieve(remote_name, temp_zip_name)
#local_size = pathlib.Path(data_path + filename).stat().st_size
else:
print 'skipping ' + temp_zip_name
鉴于我来自 UNIX,data_path 具有值
>>> data_path
Out[3]: '/data/cew/'
有趣的是,该脚本仍在 windows 下运行。我的文件按预期放在那里:
>>> os.listdir('/data/cew')
Out[9]:
['2005.annual.singlefile.csv',
'2005.csv',
'2005.zip',
'2005_filtered.csv',
'2006.annual.singlefile.csv',
'2006.csv',
'2006.zip',
'2006_filtered.csv']
但是,我不知道那在哪里。 Python 是否将这些保存在我的用户目录中的某个位置?自然,这个文件夹不存在。尝试cd /data/cew 在 Windows 的 cmd 中给了我一个错误。
【问题讨论】:
-
几乎可以肯定是
C:/data/cew。 -
@MartijnPieters 是的。这是因为
C:在驱动器列表中排在第一位,因为它是 WINDOWS 驱动器,还是我怎么能期望它在其他基于 Windows 的系统上工作? -
我不确定;我很少使用 Windows。也许Find System Hard Disk Drive from Python?
-
/data/cew是 Windows 中的绝对但不是完全限定的路径。系统通过使用当前工作目录的驱动器或网络共享来完全解决它。 API 还会为您将正斜杠转换为反斜杠。 -
@eryksun 我觉得这是一个令人满意的答案。