问题描述
以下是我的情况:
我正在尝试以编程方式访问存储在Sun OS 5.8文件服务器上的IBM AIX
系统上创建的文件,通过一个samba映射的驱动器
Win32系统上的
。不要混淆?好吧,让我们继续...... ;-)
当我要求相关目录的os.listdir()时,我得到文件名
嵌入转义字符(例如:
'' F07JS41C.04389525AA.UPR\xa6INR.E\xa6C-P.D11.081305.P2.KPF.model '')
,读作False在应用os.path.isfile()时。我希望对这些文件应用一些操作,但是我无法,因为
python(至少在Win32上)并不认为这是有效的
文件名。
帮助我,在我的天才薄薄的外表被我老板的眼睛撕裂之前!
; - )
Here is my situation:
I am trying to programatically access files created on an IBM AIX
system, stored on a Sun OS 5.8 fileserver, through a samba-mapped drive
on a Win32 system. Not confused? OK, let''s move on... ;-)
When I ask for an os.listdir() of a relevant directory, I get filenames
with embedded escaped characters (ex.
''F07JS41C.04389525AA.UPR\xa6INR.E\xa6C-P.D11.081305.P2.KPF.model'')
which will read as "False" when applying an os.path.isfile() to it. I
wish to apply some operations to these files, but am unable, since
python (on Win32, at least) does not recognize this as a valid
filename.
Help me, before my thin veneer of genius is torn from my boss''s eyes!
;-)
推荐答案
我不确定答案,但请注意.isfile()不只是检查
文件名是否有效,它正在检查那个*存在*
这个名字,并且它是一个文件。差异很大......至少在
告诉你在哪里寻找解决方案。在这种情况下,检查
ntpath.isfile()中的两个测试中的哪一个实际上失败可能是一个
如果你没有其他一些导致的第一步。 (ntpath是os.path
转换成Windows,所以在Python lib文件夹中查找ntpath.py。)
如果你是真的看到你所看到的,我怀疑是一个错误,因为如果
os.listdir()可以找到它(而且它确实是一个文件),os.isfile()应该
将其报告为文件,我想。
-Peter
I''m not sure of the answer, but note that .isfile() is not just checking
whether the filename is valid, it''s checking that something *exists*
with that name, and that it is a file. Big difference... at least in
telling you where to look for the solution. In this case, checking
which of the two tests in ntpath.isfile() is actually failing might be a
first step if you don''t have some other lead. (ntpath is what os.path
translates into on Windows, so look for ntpath.py in the Python lib folder.)
If you''re really seeing what you''re seeing, I suspect a bug since if
os.listdir() can find it (and it''s really a file), os.isfile() should
report it as a file, I would think.
-Peter
如果你给os.listdir()提供一个unicode路径,问题是否仍然存在?
这将导致listdir()返回不太容易的unicode文件名
来编码混淆。
彼得
Does the problem persist if you feed os.listdir() a unicode path?
This will cause listdir() to return unicode filenames which are less prone
to encoding confusion.
Peter
只是为了消除显而易见的,你在调用isfile()之前用
父名调用os.path.join(),是吗? os.listdir(someDir)中的f为
:
fp = os.path.join(someDir,f)
如果os.path.isfile(fp):
...
Kent
Just to eliminate the obvious, you are calling os.path.join() with the
parent name before calling isfile(), yes? Something like
for f in os.listdir(someDir):
fp = os.path.join(someDir, f)
if os.path.isfile(fp):
...
Kent
这篇关于文件名编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!