【问题标题】:Get all files on disk in python在python中获取磁盘上的所有文件
【发布时间】:2021-11-10 19:25:06
【问题描述】:

我在 python 中创建脚本,获取磁盘上的所有文件,但没有文件夹,只有文件。这是我的代码。

import hashlib
import os

if os.name != "nt":
    print("Sorry this script works only on Windows!")

path = "C://"
dir_list = os.listdir(path)
print(dir_list)

【问题讨论】:

  • 您好,请问您有什么问题?您是坚持从文件夹中分辨文件,还是在其他方面?

标签: python windows file


【解决方案1】:

您可以使用例如 pathlib 库并构建类似的东西:

import pathlib

path = "" # add your path here don't forget a \ at the end on windows
for i in os.listdir(path):
    if pathlib.Path(path + i).is_dir() is not True:
         print(i)

它遍历当前目录并检查它是否是一个目录,方法是从列表条目创建一个 Path 对象,然后检查该对象是否是一个目录。

【讨论】:

  • 让我再次告诉你我想要什么:文件夹 -> file.txt -> 文件夹->anotherfile.txt 我想获取所有文件 file.txt 和 anotherfile.txt
猜你喜欢
  • 2011-04-14
  • 1970-01-01
  • 2018-10-24
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
  • 2019-08-11
  • 1970-01-01
相关资源
最近更新 更多