Jump to content

[PYTHON] Print out the content of a disk

Wictorian

How can I print the entire content of a disk? What I mean is to print out all the files and folders and all the files and folders in each folder and so on. 

 

Link to comment
Share on other sites

Link to post
Share on other sites

See Python List Files in a Directory [5 Ways] – PYnative

 

The most basic way is to make a function that has a path as parameter / input and scans the contents of a folder with that path.  Optionally, the function then takes only the folder names, adds them one by one in the path and calls itself with the new path.

You'll just want to check and ignore the folders "." and ".."  because those are "special", ".." means go back in previous folder, and "." means current folder.

Link to comment
Share on other sites

Link to post
Share on other sites

On 6/20/2022 at 3:40 PM, Sakuriru said:

Ok this is cool but I want to do this myself. I know we don't have to discover America for the second time but I am not doing this for functionality but for fun so I would rather do it myself. Thanks for the info though.

Link to comment
Share on other sites

Link to post
Share on other sites

On 6/22/2022 at 6:48 PM, Wictorian said:

Ok this is cool but I want to do this myself. I know we don't have to discover America for the second time but I am not doing this for functionality but for fun so I would rather do it myself. Thanks for the info though.

You could use os.listdir() and os.stat() if you wanted to do it yourself:
https://docs.python.org/3/library/os.html#os.listdir

https://docs.python.org/3/library/os.html#os.stat

(os.listdir was already in the link sent earlier but you did not reply to that one)

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, mikat said:

You could use os.listdir() and os.stat() if you wanted to do it yourself:
https://docs.python.org/3/library/os.html#os.listdir

https://docs.python.org/3/library/os.html#os.stat

(os.listdir was already in the link sent earlier but you did not reply to that one)

how will os.stat() be useful though?

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

you can use the function listdir and pass you folder location

from os import listdir as list

files = list('.') ## . represent the current folder
print(files) ## will return the file and folder inside the target

or the walk function 

from os import walk

for currentPath, folders, files in walk('.'): ## . represent the current folder
	print(currentPath) ## will return the path of everything inside the target

 

the difference between these 2 is mostly that listdir() is not recursive and will return the direct children of the folder, while walk will() run into ever folder/subfolder/files

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×