Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading / printing dir contents 1

Status
Not open for further replies.

Lozbinator

Programmer
Jan 13, 2003
50
AU
Hi,

I'm new to this list and to python.

I am wondering, am I able to make my program read the file names in a directory and then print each file name to a new line in a text file?

If it is possible I'd appreciate some help.

Thank you!
 
## using Python 2.2.2
import os

sPath = "C:/Windows/"
sFile = "C:/Windows/Temp/delme.txt"

files = os.listdir(sPath)

f = file(sFile, "w")
for name in files:
if os.path.isfile(sPath + name):
f.write(name + "\n")
f.close()

print "finished!"
 
Thank you for your help. Given that I am still so new, would you mind explaining how this works?
 
## I too am new to Python
## However, I will try my best to explain the code

files = os.listdir(sPath)
## this line puts the names of all the files and subdirectories in the specified path/directory into a list variable named 'files'

f = file(sFile, "w")
## this opens the specified file for output so that we could later write to it
## the file object is stored in the variable named 'f'

for name in files:
if os.path.isfile(sPath + name):
f.write(name + "\n")
## the 'for ...' loop is to iterate through all elements of the list variable named 'files'
## each element of the list can then be referenced via the variable named 'name'
## the 'if ...' checks if the element is a file and not a subdirectory
## f.write(name + "\n") means write contents of the variable 'name' into the file 'f'
## the "\n" is a line separator
 
Thanks again!

Python is my first attempt at learning to code properly since my limited programming experience @ uni in '98.

Since your last post I was playing around with trying to get the 'last modified' date to print too.

All fun and games!
 
open a python command-line prompt
enter the following

>>>import os
>>>help(os.path)

browse for functions starting with 'g'
you'll find what you need

I would like to have more time to explore Python more but work takes too much of my time as it is.

Enjoy coding! Python is great! I wish I started programming in Python instead of VB. Bye! Got to get back to work.
 
As JustinEzequiel wrote, listdir() works very well.

Alternatively, you can use the glob module:

It's especially usefull when you want to filter filenames.

Example: List all the EXE files in the c:\Windows directory:
import glob
file = open('myfiles.txt','w+b')
filelist = glob.glob(r'c:\windows\*.exe')
file.write( '\n'.join(filelist) )
file.close()
 
'w' means "open file for writing"
'w+' means "open file for writing, but empty the file if it already exists."
'b' means "in binary mode".

In text mode, Python will write a newline to the file for each line sent to text file.
In binary mode, Python treats the file as a stream of bytes (Therefore I manually add newlines ('\n'.join...)).
 
I am also new to python, and when I tryed to use this code it told me that the file was uncallable.

f = file(sFile, "w")--------the file right here------

and when I would switch that file to open, it would work fine but the file that it is writing to would be empty.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top