0

I would like to make a program in python 2.7 that makes a list of all the files in the folder I'm in. I want it to print the name, size, writes "f" if it's a file and writes "d" if it's a folder. Are there anyone that could help me, would be very grateful!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, stack traces, compiler errors - whatever is applicable). The more detail you provide, the more answers you are likely to receive. – Martijn Pieters Jun 22 '13 at 23:09

2 Answers2

1

well, here's your answer, but I can only second what @MartijnPieters said... Try to do something first!

for f in os.listdir('./'): 
    print f, os.path.getsize(f), 'd' if os.path.isdir(f) else 'f'

and here are a few hints you could have found yourself:

Community
  • 1
  • 1
zmo
  • 24,463
  • 4
  • 54
  • 90
  • Thank you, I'l try it! :D And hopefully I won't mess up to bad!!!! :D By the way, os.listdir() is that something I have to import or is it already a function? – Johan Lundgren Jun 23 '13 at 23:12
  • Ok, it worked! I think... :) But if I'm allowed to get into details, what do the different os. do? I think I can understand the rest of it but it would be good to know for future use...! – Johan Lundgren Jun 23 '13 at 23:20
0

The os module is very helpful for this, particularly os.listdir.

Antimony
  • 37,781
  • 10
  • 100
  • 107