有没有办法在Python中返回当前目录中所有子目录的列表?
我知道你可以用文件做到这一点,但我需要获取目录列表.
我正在尝试编写一个简单的Python脚本,它将index.tpl复制到所有子目录中的index.html(除了少数例外).
我试图获取子目录列表而陷入困境.
我知道我们可以使用os.walk()列出目录中的所有子目录或所有文件.但是,我想列出完整的目录树内容:
如何在Python中实现这一目标?
import glob
list = glob.glob(r'*abc*.txt') + glob.glob(r'*123*.txt') + glob.glob(r'*a1b*.txt')
for i in list:
print i
Run Code Online (Sandbox Code Playgroud)
此代码用于列出当前文件夹中名称中包含"abc","123"或"a1b"的文件.如何使用一个glob来实现这个功能,谢谢
我有一个名为notes的文件夹,它们自然会被分类到文件夹中,在这些文件夹中也会有子类别的子文件夹.现在我的问题是我有一个功能,遍历3个级别的子目录:
def obtainFiles(path):
list_of_files = {}
for element in os.listdir(path):
# if the element is an html file then..
if element[-5:] == ".html":
list_of_files[element] = path + "/" + element
else: # element is a folder therefore a category
category = os.path.join(path, element)
# go through the category dir
for element_2 in os.listdir(category):
dir_level_2 = os.path.join(path,element + "/" + element_2)
if element_2[-5:] == ".html":
print "- found file: " + element_2
# add the file to the list of files …Run Code Online (Sandbox Code Playgroud) 如何列出仅来自特定目录的文件名?我不需要子目录中的文件名。
我试图用python列出目录和文件(recursivley):
./rootdir
./file1.html
./subdir1
./file2.html
./file3.html
./subdir2
./file4.html
Run Code Online (Sandbox Code Playgroud)
现在我可以很好地列出目录和文件(从这里借用它).但我想用以下格式和ORDER列出它(这对我正在做的事情非常重要.
/rootdir/
/rootdir/file1.html
/rootdir/subdir1/
/rootdir/subdir1/file2.html
/rootdir/subdir1/file3.html
/rootdir/subdir2/
/rootdir/file4.html
Run Code Online (Sandbox Code Playgroud)
我不在乎它是如何完成的.如果我走在目录中,然后组织它或按顺序获取所有内容.无论哪种方式,提前谢谢!
编辑:添加以下代码.
# list books
import os
import sys
lstFiles = []
rootdir = "/srv/http/example/www/static/dev/library/books"
# Append the directories and files to a list
for path, dirs, files in os.walk(rootdir):
#lstFiles.append(path + "/")
lstFiles.append(path)
for file in files:
lstFiles.append(os.path.join(path, file))
# Open the file for writing
f = open("sidebar.html", "w")
f.write("<ul>")
for item in lstFiles:
splitfile = os.path.split(item)
webpyPath = splitfile[0].replace("/srv/http/example/www", "") …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用find命令从我的工作目录中查找所有文件'M*'并按目录顺序显示结果.
相反,它会按排序顺序显示结果,这会导致首先列出一些更深的目录,因为它们按字母顺序依次排列.
$ find -name 'M*'
./MyFourth
./s/MyFirst
./s/v/b/MyThird
./s/v/MySecond
Run Code Online (Sandbox Code Playgroud)
我希望它按此顺序:
./MyFourth
./s/MyFirst
./s/v/MySecond
./s/v/b/MyThird
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
背景
我一直dir/s在批处理文件中使用该命令.但是,我无法使用python调用它.注意:我使用的是Python 2.7.3.
码
import subprocess
subprocess.call(["dir/s"])
Run Code Online (Sandbox Code Playgroud)
错误信息
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
subprocess.call(["dir/s"])
File "C:\Python27\lib\subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)
我试过改变报价但没有任何效果.
我该如何dir/s使用模块subprocess?
如何将.zip带有Python 的文件解压缩到某个目录中,output_dir并获取解压缩所产生的所有目录的列表?例如,如果我有:
unzip('myzip.zip', 'outdir')
outdir是一个可能包含其他文件/目录的目录.当我解压缩myzip.zip它时,我想unzip返回outdir/由于压缩而产生的所有目录.到目前为止,这是我的代码:
import zipfile
def unzip(zip_file, outdir):
"""
Unzip a given 'zip_file' into the output directory 'outdir'.
"""
zf = zipfile.ZipFile(zip_file, "r")
zf.extractall(outdir)
Run Code Online (Sandbox Code Playgroud)
如何unzip返回它创建的目录outdir?谢谢.
编辑:对我来说最有意义的解决方案是只获取zip文件中的顶级目录,然后递归遍历它们,这将保证我获得zip所生成的所有文件.这可能吗?名单的系统特定行为使其几乎不可能依赖
可以说我的结构是这样的
/-- am here
/one/some/dir
/two
/three/has/many/leaves
/hello/world
Run Code Online (Sandbox Code Playgroud)
并说/ one/some/dir包含一个大文件,500mb和/ three/has/many/leaves在每个文件夹中包含一个400mb文件.
我想生成每个目录的大小,以获得此输出
/ - in total for all
/one/some/dir 500mb
/two 0
/three/has/many/leaved - 400mb
/three/has/many 800
/three/has/ 800+someotherbigfilehere
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
python ×10
file ×2
filesystems ×2
linux ×2
compression ×1
directory ×1
filenames ×1
glob ×1
python-2.7 ×1
python-3.x ×1
subdirectory ×1
subprocess ×1
traversal ×1
windows ×1
zipfile ×1