Pointers

A forum for general discussion of the Python programming language.

Pointers

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

All,

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?

Thanks,

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'):
            lines.append(line)
        g.write(''.join(lines[98:2476]))
g.close()
waltergronkite
 
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:
                    outfile.write(line)

another method using fileinput
Code: Select all
import fileinput
with open(outfilename, 'w') as fout:
    for line in fileinput.input(filenames):
        fout.write(line)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
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.
ochichinyezaboombwa
 
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 3 guests