如何在Windows中的字符串列表中查找文件路径,而不考虑字符串中斜杠的方向

2024-10-02 02:38:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在Windows中,使用Python 2.7读取文件的内容,并将该文件中的某些行(在前面加上字符串“D:\abcddev\”)放入名为FilePathList的列表中。 这些行是文件的路径,例如:

D:\abcddev\toeblog/folderX/fileA.h
D:\abcddev\toeblog/folderY/fileB.h

您将注意到路径包含向前和向后斜杠的混合。不幸的是,我对此无能为力,它们就是这样创建的,我只有在那之后才能访问它们

我想检查是否在列表中找到某个路径。路径包含所有向后斜杠

因此,继续本例,我想检查上面的列表中是否有以下内容:

D:\abcddev\toeblog\folderY\fileB.h

如您所见,此字符串包含所有向后斜杠

所以我的问题是,无论斜杠是正斜杠还是反斜杠,如何检查是否相等

我的想法是将FilePathList的所有成员转换为以反斜杠分隔的路径,并将它们放入一个新的列表NormalizedFilePathList中,然后在该列表中搜索我希望找到的路径

这是我的代码:

# Declare list
NormalizedFilePathList = []

# Add backward slash separated lines to NormalizedFilePathList
for file in FilePathList:
    NormalizedFilePathList.append (os.path.normpath(file)) 

# Display the contents of NormalizedFilePathList
for file in NormalizedFilePathList
    print file

# Create the string to be searched for
test_file = 'D:\abcddev\toeblog\folderY\fileB.h'

# Search for the string in NormalizedPathFileList
if test_file in NormalizedFilePathList:
    print "Found test_file"
else:
    print "Did not find test_file"

以下是上述结果的输出:

D:\abcddev\toeblog\folderX\fileA.h
D:\abcddev\toeblog\folderY\fileB.h
Did not find test_file

为什么这不起作用?显然存在与“D:\abcddev\toeblog\folderY\fileB.h”匹配的项

我在困惑中尝试了以下几件事来澄清问题:

  1. 使用repr()打印NormalizedPathFileList中的字符串,查看是否有隐藏字符阻止找到匹配项。不,没有

  2. 人工创建了一个新列表,我手动填充并搜索了它

ManualList = ['D:\abcddev\toeblog\folderX\fileA.h','D:\abcddev\toeblog\folderY\fileB.h']

for file in ManualList
    print file

# Search for the string in ManualList
if test_file in ManualList:
    print "Found test_file"
else:
    print "Did not find test_file"

结果如下:

D:\abcddev    oeblog\folderX\fileA.h
D:\abcddev    oeblog\folderY\fileB.h
Found test_file

正如你所看到的,中间有一个制表符。 这是因为字符串包含“\t”

如果出于同样的原因打印出test_文件,我还会看到:

D:\abcddev    oeblog\folderY\fileB.h

这解释了为什么我手动创建字符串时搜索会起作用

所以问题是如何转义test\u文件字符串中的\t字符

请注意,我编写的任何代码都必须在Linux中工作


Tags: 文件字符串intest路径列表forfile
2条回答

删除斜线并进行比较怎么样

def strip_slashes(path):
  return path.replace('/','').replace('\\','')

paths = ['D:\\p1\\p2/folderY/fileB.h','D:\\p1\\p2/folderX/fileA.h']
stripped_paths = [strip_slashes(p) for p in paths]
path_to_find_1 = 'D:\\p1\p2\\folderY\\fileB.h'
stripped_path_to_find_1 = strip_slashes(path_to_find_1)
path_to_find_2 = 'D:\\p1\p452\\folderY\\fileB.h'
stripped_path_to_find_2 = strip_slashes(path_to_find_2)


print('        ')

print(stripped_path_to_find_1 in stripped_paths)
print(stripped_path_to_find_2 in stripped_paths)

您遇到了问题,因为反斜杠表示escape characters。例如,正如您所发现的,\t是一个选项卡,但Python也将\a\f视为转义字符。事实证明,它们分别代表ASCII bell和form feed。谁知道呢?一种解决方案是使用原始字符串,在字符串的引号前用r表示,它不会检查转义字符,并将反斜杠视为纯文本。否则,您需要写入\\以显示反斜杠

而且,os.path.normpath{a2}并且不会在Linux中执行您需要的操作,因此您还需要一个replace。一般来说,如果必须选择所有正向斜杠或所有反向斜杠,请选择所有正向斜杠,因为Windows can handle forward slashes而其他操作系统无法处理反向斜杠

# Declare list    
ManualList = [r'D:\abcddev\toeblog/folderX/fileA.h',r'D:\abcddev\toeblog/folderY/fileB.h']
NormalizedFilePathList = []

# Add standardized slash separated lines to NormalizedFilePathList
for file in ManualList:
    NormalizedFilePathList.append (os.path.normpath(file.replace('\\', '/')))

# Display the contents of NormalizedFilePathList
for file in NormalizedFilePathList:
    print file

# Create the string to be searched for. 
# Use forward slashes in the string below to preserve compatibility for Linux. 
# normpath will convert them to backslashes on Windows.
test_file = os.path.normpath('D:/abcddev/toeblog/folderY/fileB.h')

# Search for the string in NormalizedPathFileList
if test_file in NormalizedFilePathList:
    print "Found test_file"
else:
    print "Did not find test_file"

相关问题 更多 >

    热门问题