How to iterate all files and directories under a directory in Python?

How to iterate all files and directories under a directory in Python?

For example, I would like to iterate each file and directory under /mnt/data/

/mnt/data/
|-- file.txt
`-- great

That is:

/mnt/data/files.txt
/mnt/data/great

You can use the os.walk to do this:

root, dirs, files = os.walk(path).next()

root will be the path.
dirs will contain the directories.
files will contain the files.

Similar Posts

  • How to convert a ps file to a pdf file

    How to convert a ps file to a pdf file that is available for publish (embedded fonts, etc)? Convert the file.ps ps file to file.pdf: $ ps2pdf -dPDFSETTINGS=/printer file.ps file.pdf</pre> Embedding Fonts in PDFs with pdflatex by Jeffrey P. Bigham: http://www.manticmoo.com/articles/jeff/programming/latex/embedding-fonts-with-pdflatex.php Read more: How to merge multiple PDF files to a PDF on Linux? How…

  • Customizing Fonts in Gnome 3

    Gnome 3 is great and I can understand that it emphasizes on simplicity. However, the configuration settings for Gnome 3 does not provide tools for certain functions that I need. One of them is customizing the fonts. This post will summarize the tools/tips that I adopted to customize the fonts in Gnome 3. Usually, the…

  • Managing LVM

    Any tutorials or tips on how to manage LVM? LVM provides a flexible and easy way to management storage disks on Linux. It also provides a set of tools for management. Here are some tutorials and references for managing LVM: LVM man pages: https://www.systutorials.com/docs/linux/man/8-lvm/#lbAL Manage Disk Volume in Linux: http://www.hongkedavid.com/blog/disk_volume.html LVM with Xen: https://www.systutorials.com/b/linux/tag/lvm/ Remove missing phyiscal volumes…

  • |

    A List of Common Software on Windows

    Quick links to some useful software on Windows including Web browsers, Video/audio player and ssh/scpsftp/vnc tools for connecting to Linux. ssh/scp/sftp/vnc for connecting to Linux For Linux users, it is sometimes unavoidably to use Windows. With Windows, the Linux users will need to connect to Linux for some tasks. Some general software are needed. The…

Leave a Reply

Your email address will not be published. Required fields are marked *