40

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:\text\*.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:\text\somefile.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:\irc\as\*.txt"
txt = glob.glob(filepath)

lines = string.split(txt, '\n') #AttributeError: 'list' object has no attribute 'split'
print lines
congusbongus
  • 13,359
  • 7
  • 71
  • 99
greg
  • 401
  • 1
  • 4
  • 4

5 Answers5

60
import os
import re
path = "/home/mypath"
for filename in os.listdir(path):
    if re.match("text\d+.txt", filename):
        with open(os.path.join(path, filename), 'r') as f:
            for line in f:
                print line,

Although you ignored my perfectly fine solution, here you go:

import glob
path = "/home/mydir/*.txt"
for filename in glob.glob(path):
    with open(filename, 'r') as f:
        for line in f:
            print line,
PeterB
  • 2,212
  • 2
  • 21
  • 33
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • 5
    In the second code snippet it should be: with open(file,'r') as f: (also a nitpick better to use a different variable name than 'file') – sateesh Feb 16 '11 at 08:00
15

You can use the glob module to get a list of files for wildcards:

File Wildcards

Then you just do a for-loop over this list and you are done:

filepath = "F:\irc\as\*.txt"
txt = glob.glob(filepath)
for textfile in txt:
  f = open(textfile, 'r') #Maybe you need a os.joinpath here, see Uku Loskit's answer, I don't have a python interpreter at hand
  for line in f:
    print line,
Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
  • As I said, you have to loop over the list you get out of glob.glob: `filepath = "F:\irc\as\*.txt" txt = glob.glob(filepath) for textfile in txt: f = open(textfile, 'r') for line in f: print line,` – Ocaso Protal Feb 16 '11 at 07:36
  • 1
    os.path.join is not needed because glob.glob gives a full path. Also consider using the with expression, in your solution the file handle isn't closed. with does this automatically :) – Uku Loskit Feb 16 '11 at 07:45
6

This code accounts for both issues in the initial question: seeks for the .txt file in the current directory and then allows the user to search for some expression with the regex

#! /usr/bin/python3
# regex search.py - opens all .txt files in a folder and searches for any line
# that matches a user-supplied regular expression

import re, os

def search(regex, txt):
    searchRegex = re.compile(regex, re.I)
    result = searchRegex.findall(txt)
    print(result)

user_search = input('Enter the regular expression\n')

path = os.getcwd()
folder = os.listdir(path)

for file in folder:
    if file.endswith('.txt'):
        print(os.path.join(path, file))
        txtfile = open(os.path.join(path, file), 'r+')
        msg = txtfile.read()
search(user_search, msg)
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
3

This problem just came up for me and I was able to fix it with pure python:

Link to the python docs is found here: 10.8. fnmatch — Unix filename pattern matching

Quote: "This example will print all file names in the current directory with the extension .txt:"

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print(file)
rAntonioH
  • 129
  • 2
  • 12
  • The '.' in os.listdir means 'current directory'. Good to know if you need to specify a directory elsewhere. – rAntonioH Nov 13 '17 at 19:49
3

Check out "glob — Unix style pathname pattern expansion"

http://docs.python.org/library/glob.html

dkamins
  • 21,450
  • 7
  • 55
  • 59
  • @greg Why are you trying string.split() on the result of glob.glob? glob.glob returns a list of matching filenames. Just loop over the list. You still need to open each file and read it. – dkamins Feb 17 '11 at 03:25