相关疑难解决方法(0)

获取当前目录中所有子目录的列表

有没有办法在Python中返回当前目录中所有子目录的列表?

我知道你可以用文件做到这一点,但我需要获取目录列表.

python directory subdirectory

467
推荐指数
22
解决办法
62万
查看次数

如何在Python中获取所有直接子目录

我正在尝试编写一个简单的Python脚本,它将index.tpl复制到所有子目录中的index.html(除了少数例外).

我试图获取子目录列表而陷入困境.

python file

134
推荐指数
8
解决办法
13万
查看次数

列出python中的目录树结构?

我知道我们可以使用os.walk()列出目录中的所有子目录或所有文件.但是,我想列出完整的目录树内容:

  • 子目录1:
    • FILE11
    • file12
    • 子目录11:
      • file111
      • file112
  • 子目录2:
    • file21
    • 子目录21
    • 子目录22
      • 子子目录221
        • 文件2211

如何在Python中实现这一目标?

python traversal directory-structure

84
推荐指数
9
解决办法
9万
查看次数

使用python的glob.glob中的正则表达式

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来实现这个功能,谢谢

python glob

31
推荐指数
3
解决办法
5万
查看次数

你如何使用python浏览目录?

我有一个名为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

18
推荐指数
2
解决办法
4万
查看次数

特定目录中的Python3列表文件

如何列出仅来自特定目录的文件名?我不需要子目录中的文件名。

python linux filenames file python-3.x

9
推荐指数
1
解决办法
2万
查看次数

在Python中创建树型目录列表

我试图用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)

python filesystems

5
推荐指数
1
解决办法
4219
查看次数

按目录顺序查找命令列表结果

我正在尝试使用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)

谢谢你的帮助

linux

5
推荐指数
1
解决办法
6994
查看次数

如何在Python中使用dir/s命令?

背景

我一直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

python windows subprocess python-2.7

4
推荐指数
2
解决办法
7053
查看次数

使用Python解压缩文件并返回它创建的所有目录

如何将.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所生成的所有文件.这可能吗?名单的系统特定行为使其几乎不可能依赖

python compression zipfile

3
推荐指数
1
解决办法
6709
查看次数

如何在python中递归生成目录大小,如du.呢?

可以说我的结构是这样的

/-- 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 filesystems operating-system

0
推荐指数
1
解决办法
1万
查看次数