簡體   English   中英

為什么我在 python2.7 中導入 os.walk 時出錯

[英]Why am I getting an error importing os.walk in python2.7

我需要在 Linux 服務器上遞歸搜索目錄/子目錄以查找特定名稱的目錄並檢索這些特定目錄中的文件。 我嘗試了兩種方法,一種是導入 os 並調用 os.walk,另一種是從 os 導入 walk。 代碼如下:

def getDeployedLibraries():
        serverConfig()
        path = 'somePath'
        deployments = cmo.getLibraries()
        print(divider)
        print("Library Deployments:" )
        print(divider)
        if deployments:
                deployedLibs = []
                stagedLibs = []
                archiveLibs = []
                for dep in deployments:
                        full_name = dep.getName()
                        path = dep.getAbsoluteSourcePath()
                        deployedLibs.append(path+full_name)
                for (dirpath, dirnames, files) in os.walk(path):
                        for name in dirnames:
                                if name != "shared-lib":
                                        dirnames.remove(name)
                        for file in files:
                                stagedLibs.append(file)
                for sLib in stagedLibs:
                        if sLib not in deployedLibs:
                                archiveLibs.append(sLib)
                f = open("filesToArchive.txt","w")
                f.write("\n".join(archiveLibs))
                f.close()
        else:
                deploymentsList.append("No deployments of this type installed.")
        domainConfig()
        return deploymentsList 

當只導入 os 並調用 os.walk 時,我收到錯誤 AttributeError: class 'org.python.modules.os' has no attribute 'walk',當我從 os 導入 walk 並調用 os 時,我收到錯誤 'ImportError : 無法導入名稱 walk'。

這個腳本確實連接到 WLST,所以我想我可能遇到了沖突,但沒有找到任何表明這種情況的東西。

該函數應該從os.path導入:

from os.path import walk

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM