题
我正在尝试编写一个简单的Python脚本,它将index.tpl复制到所有子目录中的index.html(除了少数例外)。
我试图获取子目录列表而陷入困境。
解决方案
import os
def get_immediate_subdirectories(a_dir):
return [name for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
其他提示
为什么没有人提到 glob
? glob
允许您使用Unix风格的路径名扩展,并且几乎所有需要查找多个路径名的内容都可以使用。这很容易:
from glob import glob
paths = glob('*/')
请注意, glob
将返回带有最终斜杠的目录(如unix所示),而大多数基于 path
的解决方案将省略最终斜杠。
import os, os.path
获取目录中的(完整路径)直接子目录:
def SubDirPath (d):
return filter(os.path.isdir, [os.path.join(d,f) for f in os.listdir(d)])
获取最新(最新)子目录:
def LatestDirectory (d):
return max(SubDirPath(d), key=os.path.getmtime)
直接来自文档:
walk()通过从上到下或向下走树来生成目录树中的文件名。对于以目录top(包括top本身)为根的树中的每个目录,它会产生一个3元组(dirpath,dirnames,filenames)。
这种方法可以一气呵成。
from glob import glob
subd = [s.rstrip("/") for s in glob(parent_dir+"*/")]
使用Twisted的FilePath模块:
from twisted.python.filepath import FilePath
def subdirs(pathObj):
for subpath in pathObj.walk():
if subpath.isdir():
yield subpath
if __name__ == '__main__':
for subdir in subdirs(FilePath(".")):
print "Subdirectory:", subdir
由于一些评论者已经问过使用Twisted库的优点是什么,我将在这里略微超出原来的问题。
一些改进的文档在解释FilePath优势的分支中;你可能想读一读。
更具体地说,在此示例中:与标准库版本不同,此功能可以使用 no imports 实现。 “子目标”函数是完全通用的,因为它只运行其参数。要使用标准库复制和移动文件,您需要依赖于“ open
”。 builtin,“ listdir
”,也许是“ isdir
”或“ os.walk
”或“ shutil.copy
”。也许是“ os.path.join
”太。更不用说你需要一个传递参数的字符串来识别实际文件。让我们来看看完整的实现,它将复制每个目录的“index.tpl”。到“index.html”:
def copyTemplates(topdir):
for subdir in subdirs(topdir):
tpl = subdir.child("index.tpl")
if tpl.exists():
tpl.copyTo(subdir.child("index.html"))
“子目标”上面的函数可以在任何 FilePath
类对象上运行。其中包括 ZipPath
对象。不幸的是, ZipPath
现在是只读的,但它可以扩展为支持写作。
您也可以传递自己的对象以进行测试。为了测试这里建议的os.path-using API,你必须使用导入的名称和隐式依赖项,并且通常执行黑魔法以使测试工作。使用FilePath,您可以执行以下操作:
class MyFakePath:
def child(self, name):
"Return an appropriate child object"
def walk(self):
"Return an iterable of MyFakePath objects"
def exists(self):
"Return true or false, as appropriate to the test"
def isdir(self):
"Return true or false, as appropriate to the test"
...
subdirs(MyFakePath(...))
我刚刚编写了一些代码来移动vmware虚拟机,最后使用 os.path
和 shutil
来完成子目录之间的文件复制。
def copy_client_files (file_src, file_dst):
for file in os.listdir(file_src):
print "Copying file: %s" % file
shutil.copy(os.path.join(file_src, file), os.path.join(file_dst, file))
它不是非常优雅,但确实有效。
这是一种方式:
import os
import shutil
def copy_over(path, from_name, to_name):
for path, dirname, fnames in os.walk(path):
for fname in fnames:
if fname == from_name:
shutil.copy(os.path.join(path, from_name), os.path.join(path, to_name))
copy_over('.', 'index.tpl', 'index.html')
def get_folders_in_directories_recursively(self, directory, index=0):
folder_list = list()
parent_directory = directory
for path, subdirs, _ in os.walk(directory):
if not index:
for sdirs in subdirs:
folder_path = "{}/{}".format(path, sdirs)
folder_list.append(folder_path)
elif path[len(parent_directory):].count('/') + 1 == index:
for sdirs in subdirs:
folder_path = "{}/{}".format(path, sdirs)
folder_list.append(folder_path)
return folder_list
以下功能可以称为:
get_folders_in_directories_recursively(directory,index = 1) - >给出第一级文件夹列表
get_folders_in_directories_recursively(目录) - >给出所有子文件夹
我必须提一下我使用的 path.py 库经常。
获取直接子目录变得如此简单:
<代码> my_dir.dirs()代码>
完整的工作示例是:
from path import Path
my_directory = Path("path/to/my/directory")
subdirs = my_directory.dirs()
注意:my_directory仍可以作为字符串进行操作,因为Path是字符串的子类,但提供了一堆有用的方法来操作路径
import glob
import os
def child_dirs(path):
cd = os.getcwd() # save the current working directory
os.chdir(path) # change directory
dirs = glob.glob("*/") # get all the subdirectories
os.chdir(cd) # change directory to the script original location
return dirs
child_dirs
函数将路径作为目录,并返回 immediate子目录的列表。
dir
|
-- dir_1
-- dir_2
child_dirs('dir') -> ['dir_1', 'dir_2']
import pathlib
def list_dir(dir):
path = pathlib.Path(dir)
dir = []
try:
for item in path.iterdir():
if item.is_dir():
dir.append(item)
return dir
except FileNotFoundError:
print('Invalid directory')