r/Python • u/[deleted] • Oct 25 '14
[Help] Need to create a script that lists directories and files
Hello! I am trying to learn Python and need to create a script that lists directories and files like dir does. Could someone please help me? Thanks! :)
Edit: Everything I say gets downvoted but I apologized here.
3
Oct 25 '14
Look into os.walk, I believe it's what you're looking for.
-4
Oct 25 '14
Thanks! :) Can you give me an example?
3
Oct 25 '14
There's an example in the link that I gave you, and I believe there's a few in the official documentation. Give it a look over and come up with something that works for you. :)
-1
Oct 25 '14
The documentation is really difficult for me to understand so I'll look into getting one or two books. Python was waaay more difficult than I imagined it would be. I guess I got into the deep end of the pool, I'll try something simpler to start with. Thank you for your time!
2
0
Oct 25 '14
hi pls try
!/usr/bin/python
from os import listdir from os.path import isfile files = [i for i in listdir('.') if isfile(f)] for i in files: print(f)
-4
Oct 25 '14
I can't get this to run? :(
0
Oct 25 '14
sry reddit is wierd #!/usr/bin/env python3 from os import listdir from os.path import isfile files = [f for f in listdir('.') if isfile(f)] for f in files: print(f)-5
Oct 25 '14
Thanks! This doesn't list directories though, can you make it do that?
-1
Oct 25 '14
#!/usr/bin/env python3 from os import listdir from os.path import isfile files = [f for f in listdir('.')] for f in files: print(f)-1
0
-1
6
u/MagicBladder Oct 25 '14
http://mattgemmell.com/what-have-you-tried/