不能让简单的循环正常工作

2024-09-28 18:55:33 发布

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

def getMSTestPath(testPath):
    dllFilePath = r'C:\Users\bgbesase\Documents\Brent\Code\Visual Studio'
    msTestFilePath = []
    dllConvert = []
    full_dllPath = []
    for r, d, f in os.walk(testPath):
        for files in f:
            if files.endswith('.UnitTests.vbproj'):
                #testPath = os.path.abspath(files)
                testPath = files.strip('.vbproj')
                msTestFilePath.append(testPath)
                #print testPath
                #print msTestFilePath

    for lines in msTestFilePath:
        ss = lines.replace(r'.', r'-')
        #print ss
        dllConvert.append(ss)

    for lines in testPath:

        dllFilePath = dllFilePath + '' + lines + '\bin\Debug' + '.dll' + '\n'
        full_dllPath.append(dllFilePath)
        print full_dllPath

    msTestFilePath = [str(value[1]) for value in msTestFilePath]
    return msTestFilePath


testPath = [blah.APDS.UnitTests
blah.DatabaseAPI.UnitTests
blah.DataManagement.UnitTests
blah.FormControls.UnitTests ]

ss = [ blah-APDS-UnitTests
blah-DatabaseAPI-UnitTests
blah-DataManagement-UnitTests
blah-FormControls-UnitTests ] 

我需要遍历路径,首先:获取以.UnitTests结尾的所有文件,并将它们作为列表testPath返回。然后,我必须将所有的.转换成-,并将该列表作为ss返回。你知道吗

这就是我被卡住的地方,我需要经过一个循环,因为testPath中有很多元组,我需要添加'dllFilePath+testPath+'\bin\Debug\'+ss+'.dll'

然而,我不能让它工作,我不知道为什么,输出只是一些废话,:( 谢谢你的帮助。你知道吗


Tags: inforfilesssfullunittestsblahlines
1条回答
网友
1楼 · 发布于 2024-09-28 18:55:33

不要使用.strip();它将其参数视为字符集,而不是特定的序列。你知道吗

因此,您正在删除集合{'.', 'v', 'b', 'p', 'r', 'o', 'j'}中的所有字符,而且删除的字符远远超出您的想象:

>>> 'blah.APDS.UnitTests.vbproj'.strip('.vbproj')
'lah.APDS.UnitTests'    # Note that 'b' was removed from the start

使用字符串切片:

testPath = files[:-len('.vbproj')]

或者使用os.path.splitext()

testPath = os.path.splitext(files)[0]

演示:

>>> 'blah.APDS.UnitTests.vbproj'[:-len('.vbproj')]
'blah.APDS.UnitTests'
>>> import os.path
>>> os.path.splitext('blah.APDS.UnitTests.vbproj')[0]
'blah.APDS.UnitTests'

相关问题 更多 >