A forum for general discussion of the Python programming language.


Postby waltergronkite » Wed Jun 05, 2013 6:23 pm


I wrote a script in python a few years ago and havent since used python at all. The script joins text files together into a larger text file. The old text files were the same length and so i could indicate the lines i wanted to append onto the large text file. I now would like to make a larger text file from text files of varying lengths.

I realise this is a simple edit but everything i have tried has not worked. Can somebody point me in the right direction?


Code: Select all
# Name: xxxxxx
# Author: xxxxxx

import os

g = open('./data/EPRI_all.log', 'a')

for p in os.listdir('./data/'): ######     
    if '.txt' in p and p[0:2] == 'Se':
        lines = []
        for line in open('./data/%s' % p, 'r'):
Posts: 3
Joined: Wed Jun 05, 2013 6:18 pm

Re: Pointers

Postby metulburr » Wed Jun 05, 2013 6:48 pm

one method:
Code: Select all
import os
dir_path = '.'
with open('output.txt', 'w') as outfile:
    for fname in os.listdir(dir_path):
        if os.path.splitext(fname)[1] == '.txt':
            with open(fname) as infile:
                for line in infile:

another method using fileinput
Code: Select all
import fileinput
with open(outfilename, 'w') as fout:
    for line in fileinput.input(filenames):
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Pointers

Postby ochichinyezaboombwa » Thu Jun 06, 2013 9:57 pm

1) You don't need to program to concatenate files. Try at your shell prompt:
cat ./data/Se*.txt > your_target_file

2) For programmers, a pointer is a variable that holds the address-of a variable stored in computer memory; so the title of your post is very confusing.
Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

Return to General Discussions

Who is online

Users browsing this forum: No registered users and 6 guests