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: 1
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):
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
User avatar
Posts: 1096
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: 200
Joined: Tue Jun 04, 2013 7:53 pm

Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest