以递归方式获取文件夹和子文件夹下的所有文件,而无需子文件夹本身的路径

seVo1d

我想递归列出文件夹及其所有子文件夹下文件的所有完整路径。有没有办法做到这一点?似乎如果文件进入 2 级,代码可以这样写,

import os


folderPATH = r'C:\Users\Arman\Desktop\Cosmology\Articles'
filePATHS = [x[2] for x in os.walk(folderPATH)]

for i in filePATHS:
    for j in i:
        print(j)

印刷

Astrophysical Constants And Parameters.pdf
desktop.ini
Physics Latex Manuel.pdf
Spactimes.pdf
A parametric reconstruction of the cosmological jerk from diverse observational data.pdf
A Thousand Problems in Cosmology Horizons.pdf
An Almost Isotropic CM Temperature Does Not Imply An Almost Isotropic Universe.pdf
Big Bang Cosmology - Review.pdf
desktop.ini
Expanding Confusion common misconceptions of cosmological horizons and the superluminal expansion of the universe.pdf
Hubble Radius.pdf
Is the Universe homogeneous.pdf
LCDM and Mond.pdf
Near galaxy clusters.pdf
The Cosmological Constant and Dark Energy.pdf
The mass of the Milky Way from satellite dynamic.pdf
The Status of Cosmic Topology after Planck Data.pdf
An upper limit to the central density of dark matter haloes from consistency with the presence of massive central black holes.pdf
Dark Matter - Review.pdf
Dark Matter Accretion into Supermassive Black Holes.pdf
desktop.ini
Andrew H. Jaffe - Cosmology - Imperial College Lecture Notes - Thermodynamics and Particle Physics.pdf
Big Bang Nucleosynthesis.pdf
Claus Grupen - Astroparticle Physics - The Early Universe.pdf
Daniel Baumann - Cosmology - Mathematical Tripos III - Thermal History.pdf
desktop.ini
James Rich - Fundamentals of Cosmology - The Thermal History of the Universe.pdf
Lars Bergström, Ariel Goobar - Cosmology and Particle Astrophysics -  Thermodynamics in the Early Universe.pdf  
Steven Weinberg - Cosmology - The Early Universe.pdf
Andrei Linde - On the problem of initial conditions for inflation.pdf
...

我想要一个产生相同结果但具有递归逻辑和完整路径的函数这样对于n嵌套文件夹,我就可以找到路径。我需要这样的东西

import os

def get_all_filePATHs(folderPATH):
    ...
    return get_all_filePATHs()

folderPATH = r'C:\Users\Arman\Desktop\Cosmology\Articles'
print(get_all_filePATHs(folderPATH))

请注意,我只对文件完整路径感兴趣,而不对子文件夹的路径感兴趣,正如您从上面的示例中看到的那样。

巴马

os.walk()递归到所有子目录。每次迭代中返回的第一个元素是目录的路径,您将其与文件名连接以获得文件的完整路径。

def get_all_filePaths(folderPath):
    result = []
    for dirpath, dirnames, filenames in os.walk(folderPath):
        result.extend([os.path.join(dirpath, filename) for filename in filenames])
    return result

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

递归更改文件夹和子文件夹中的文件名,而无需更改文件路径

获取文件夹和子文件夹中所有.txt文件的路径

递归清除其中没有文件的文件夹中的所有文件夹和子文件夹

遍历文件夹和子文件夹中的所有文件并获取创建日期

获取文件夹和子文件夹中的所有文件

如何选择位于文件夹或子文件夹下的所有js文件

.docker忽略所有子文件夹,但不忽略文件夹本身中的文件

AWS CodeBuild buildspec.yml递归获取所有文件和子文件夹

在不同的命名文件夹下创建子文件夹

LibreOffice:以递归方式转换文件夹和子文件夹中的文档

Python脚本递归重命名文件夹和子文件夹中的所有文件

获取文件夹中所有文件的绝对路径,不遍历子文件夹

读取文件夹和子文件夹中的所有文件-进度和大小

Powershell:将所有文件从文件夹和子文件夹移至单个文件夹

删除所有旧文件,文件夹和子文件夹的命令

为文件夹和子文件夹中的所有文件设置命名空间

将文件复制到文件夹和所有子文件夹中

Git将文件夹视为文件,并忽略所有子文件夹和-files

从文件夹和子文件夹加载所有文件

编辑和重置所有文件夹、子文件夹和文件的权限

Xampp htdocs文件夹,子文件夹和所有文件的权限

Gitignore用于文件夹和子文件夹中的所有文件

以cmd列出所有文件夹和子文件夹,但不列出文件

批。批量重命名文件夹和所有子文件夹中的文件

如何获取没有文件的文件夹和子文件夹的列表?

C ++删除所有文件和子文件夹,但保留目录本身

Apache htaccess将根目录和所有根文件夹重写为子文件夹,而无需重定向

密码保护子域中显示的文件夹和网站,而无需在该文件夹中放置文件

Powershell 查找给定文件夹名称中的所有空文件夹和子文件夹