r/Python • • 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.

0 Upvotes

19 comments sorted by

6

u/MagicBladder Oct 25 '14

0

u/[deleted] Oct 25 '14

Whoah, okey, judging by the downvotes and messages I got I've been an ass here. In response to you and HairyScotsman - sorry. My initial thought about your reply, the implied message being that I am supposed to try and fail first (or whatever, sorry, only skimmed it through), was that you were an elitist prick who'd rather nitpick about details than actually answer my question. So I figured laughing dismissively would be a somewhat more polite response to you than telling you to piss off.

Clearly I was in the wrong about asking for this sample code without having tried anything on my own first (I mean as in literally not done squat). I was simply curious about how such a program would look. As HairyScotsman put it I simply wanted someone else to tell me how to. I understand that no matter how polite the question you need to try it on your own first when it comes to programming. So, I apologize.

-4

u/[deleted] Oct 25 '14

lol

0

u/MagicBladder Oct 25 '14

clearly a troll who has read the very article i posted. So, yes. Reddit is veeeery weird (and sometimes lazy and stupid )

0

u/[deleted] Oct 25 '14

[deleted]

-4

u/[deleted] Oct 25 '14

Doesn't help me at all. I just needed something to get me started. No need to be rude to newbies...

0

u/[deleted] Oct 25 '14

[deleted]

-1

u/[deleted] Oct 25 '14

That makes sense and see my response above. Sorry about this and thank you for explaining what I did wrong. Obviously there is more to programming than just programming! :)

3

u/[deleted] Oct 25 '14

Look into os.walk, I believe it's what you're looking for.

-4

u/[deleted] Oct 25 '14

Thanks! :) Can you give me an example?

3

u/[deleted] 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

u/[deleted] 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

u/[deleted] Oct 25 '14

No problem, I hope you manage it eventually. :)

0

u/[deleted] 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

u/[deleted] Oct 25 '14

I can't get this to run? :(

0

u/[deleted] 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

u/[deleted] Oct 25 '14

Thanks! This doesn't list directories though, can you make it do that?

-1

u/[deleted] 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

u/[deleted] Oct 25 '14

Very cool, thank you for your time!

0

u/[deleted] Oct 25 '14

why downvotes ??

-1

u/[deleted] Oct 25 '14

I apologize! Even me saying sorry gets downvoted but I'm sorry! Okey?