Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python字典,文件夹和文件的结构,但在两个字典中_Python_File_Python 3.x_Dictionary_Directory - Fatal编程技术网

Python字典,文件夹和文件的结构,但在两个字典中

Python字典,文件夹和文件的结构,但在两个字典中,python,file,python-3.x,dictionary,directory,Python,File,Python 3.x,Dictionary,Directory,我正在搜索一个算法来完成一项工作。基本上我从用户那里得到一个输入,源目录。我想创建两个不同的词典,一个保存文件夹的子文件夹,一个保存文件夹的文件。 例如: 我有一个文件夹“folder”,里面包括一个名为:log.txt的文本文件和3个子文件夹folder1、folder2和folder3。在folder2中有另一个名为txt1.txt的文本文件和另一个名为subfolder的文件夹,在该子文件夹中有一张名为pic.png的图片 folder->log.txt, folder1, fold

我正在搜索一个算法来完成一项工作。基本上我从用户那里得到一个输入,源目录。我想创建两个不同的词典,一个保存文件夹的子文件夹,一个保存文件夹的文件。 例如:

我有一个文件夹“folder”,里面包括一个名为:log.txt的文本文件和3个子文件夹folder1、folder2和folder3。在folder2中有另一个名为txt1.txt的文本文件和另一个名为subfolder的文件夹,在该子文件夹中有一张名为pic.png的图片

folder->log.txt, folder1, folder2->(txt.txt, subfolder->pic.png), folder3
我想要的是这样一本字典:

folders = {'folder1':'', 'folder2': 'subfolder', folder3}
files = {'log.txt':'', 'txt.txt': 'folder2', 'pic.png':'folder2/subfolder'}
一般来说,我需要的是文件夹和子文件夹的清晰字典,以便在创建正确的文件夹和子文件夹后轻松操作。然后,需要对文件的字典进行分类,以明确文件的归属。如果你有其他结构的字典,为文件无需担心,如果你认为是更容易和快速。 先谢谢你

编辑:我正在运行MacOSX和Python3

编辑2:

dirs = [d for d in os.listdir(source) if os.path.isdir(os.path.join(source, d))]

for folder in dirs:
    tmp_source = source + folder
    dirs2 = [d for d in os.listdir(tmp_source) if os.path.isdir(os.path.join(tmp_source, d))]
    if dirs2 != []:
       print('Folder: ', dirs2, 'is not empty')

    dic[folder] = dirs2

下面的代码将递归地遍历根文件夹,并将每个文件夹(包括根文件夹)中的文件名保存在字典中,其中键为文件夹路径

import os

root_folder = r'C:\Users\Steinar\Google Drive\Kode\Ymse\test\test'
content = {}

for root, dirs, files in os.walk(root_folder):
    for subdir in dirs:
        content[os.path.join(root, subdir)] = []
    content[root] = files

# Print out the content dict    
for folder, filenames in content.items():
    print 'Folder: {}'.format(folder)
    print 'Filenames:'
    for filename in filenames:
        print '-> {}'.format(filename)
使用上述输入,该脚本输出正确的文件夹结构

Folder: C:\Users\Steinar\Google Drive\Kode\Ymse\test\test\test2
Filenames:
-> test2.txt
Folder: C:\Users\Steinar\Google Drive\Kode\Ymse\test\test
Filenames:
-> test.txt
Folder: C:\Users\Steinar\Google Drive\Kode\Ymse\test\test\test2\test3\test4
Filenames:
Folder: C:\Users\Steinar\Google Drive\Kode\Ymse\test\test\test2\test3
Filenames:
-> test3.txt

如果您想重建文件夹结构,只需迭代
content.keys()
,然后创建每个文件夹。

谢谢,但我遇到了以下错误:回溯(最近一次调用):文件“/Users/Desktop/app.py”,第42行,在strr=os.path.walk(source\u dir,store\fnames,None)中AttributeError:“module”对象没有属性“walk”,请记住像这样解析文件夹字符串:
r'C:\path\etc\
,以及
import os
。那么,您应该检查Python安装。这段代码在Python 2.7上进行了测试。@iCyprus阅读了我和Steinar Lima评论中的两个附加链接。我更新了答案,但你真的应该自己做些事情。如果您玩弄代码,就不难理解应该如何解决这个问题。你试过什么吗?如果您需要更多帮助,请向我们提供一些代码,向我们表明您至少自己尝试过理解我们的建议。您是否复制了内容?如果是这样的话,你不是吗?我想按我的方式去做,利用字典。我不想要内置函数。先谢谢你。