I have a directory of text files that all have the extension .txt
. My goal is to print the contents of the text file. I wish to be able use the wildcard *.txt
to specify the file name I wish to open (I'm thinking along the lines of something like F:ext*.txt
?), split the lines of the text file, then print the output.
Here is an example of what I want to do, but I want to be able to change somefile
when executing my command.
f = open('F:extsomefile.txt', 'r')
for line in f:
print line,
I had checked out the glob module earlier, but I couldn't figure out how to actually do anything to the files. Here is what I came up with, not working.
filepath = "F:ircas*.txt"
txt = glob.glob(filepath)
lines = string.split(txt, '
') #AttributeError: 'list' object has no attribute 'split'
print lines
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…