Domanda

I need to scan a large filesystem (1tb) and save this to an XML file. I want this XML file to be structured as described in the code.

Does anybody know how to read a directory and all its subdirectories and files one by one (so they can be added to an XML file) in Python?

<files>
    <file id = '1'>
        <name>myfile.file</name>
        <path>path/to/file/myfile.file</path>
    </file>
</files>
È stato utile?

Soluzione

You might want to use a recursive procedure in conjunction with the os module. This module has functions for listing directory contents and telling if an item is a directory namely os.listdir, os.path.isdir and os.path.isfile. Using these you should be able to do what you are suggesting relatively easily. I do not know if you wish to preserve the directory structure or not, if not you can simply store the files and discard the directories.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top