简体   繁体   English

我想将特定文件夹中文件的所有名称添加到列表中

[英]I want to add all the names of the files in a specific folder to a list

I want to add the names of all the files in a specific folder to a list how can i do that?我想将特定文件夹中所有文件的名称添加到列表中,我该怎么做? the pathway is from dropbox -> a folder called 'UMM' -> a folder called '2018' could someone help me with the code on this.路径来自 Dropbox -> 一个名为“UMM”的文件夹 -> 一个名为“2018”的文件夹,有人可以帮我编写代码。 I have tried using os.walk() but it doesn't seem to work我试过使用 os.walk() 但它似乎不起作用

You can use os.walk and append only names which are in files.您只能使用文件中的 os.walk 和 append 名称。

from os import walk

file_names = list()
path = 'path/of/folder'

for root, dirc, files in walk(path):
    for FileName in files:
        file_names.append(FileName)

print(file_names)

This will append all the files name from all the directories and sub-directories of the specified path.这将 append 指定路径的所有目录和子目录中的所有文件名。

this will create a list of the files in a folder这将创建一个文件夹中的文件列表

from os import listdir

# the path
path = ''

fileList = listdir(path)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想用 python 重命名文件夹中的所有文件 - I want to rename all the files in a folder with python Python:搜索特定字符串的文件文件夹,然后列出包含它的所有文件 - Python: Scour a folder of files for a specific string, then list all the files containing it 我想列出特定文件夹中的目录,但我希望它们在名称前列出一个数字 - I want to list out directory in a particular folder but i want them with a number listed before their names 我有一个 python 脚本,其中列出了具有特定扩展名的文件。 我想将这些文件的文件名打印到 txt 文件中。 都在同一条路上 - I have a python script which lists files with specific extensions. I want to print the file names of those files into a txt file. All in the same path 显示特定文件夹中的所有文件名 - Display all file names from a specific folder 从文件夹中的所有文件名中删除数字 - Remove numbers from all files names in a folder 我想列出我的 sftp 服务器中的所有文件 - I want to list all the files that are in my sftp server 如何列出特定文件夹/包的所有 Python 要求? - How can I list all Python requirements for a specific folder/package? 希望列表中的名称作为使用 for 循环 python 的文件名 - want names inside list to be as names of files using for loop python 想要将文件夹及其子文件夹中的所有图像编译为带有名称的pdf文件 - Want to compile all the images in a folder and its subfolders to pdf file with names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM