为什么我在 python2.7 中导入 os.walk 时出错
Why am I getting an error importing os.walk in python2.7
我需要在 Linux 服务器上递归搜索 directories/subdirectories 特定名称的目录,并检索 ose 特定目录中的文件。我尝试了两种方法,一种是导入 os 并调用 os.walk,另一种是从 os 导入步行。代码如下:
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' 并且当我导入 walk从 os 调用 os,我得到错误 'ImportError: cannot import name walk'。
此脚本确实连接到 WLST,所以我想我可能遇到了冲突,但没有发现任何迹象表明存在冲突。
函数应从 os.path
导入:
from os.path import walk
我需要在 Linux 服务器上递归搜索 directories/subdirectories 特定名称的目录,并检索 ose 特定目录中的文件。我尝试了两种方法,一种是导入 os 并调用 os.walk,另一种是从 os 导入步行。代码如下:
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' 并且当我导入 walk从 os 调用 os,我得到错误 'ImportError: cannot import name walk'。
此脚本确实连接到 WLST,所以我想我可能遇到了冲突,但没有发现任何迹象表明存在冲突。
函数应从 os.path
导入:
from os.path import walk