7p5R0%18 Pytnon Fle Concatenation- Stack Overtow
Python File Concatenation
Ihave a data folder, with subfolders
for each subject that ran through a
program. So, for example, in the data
folder, there are folders for Bob, Fred,
and Tom. Each one of those folders
contains a variety of fles and
subfolders. However, | am only
interested in the ‘summary.og’ fle
contained in each subject's folder.
Iwant to concatenate the
‘summary.log' file from Bob, Fred, and
‘Tom into a single log fie in the data
folder. In addition, I want to add a
column to each log file that will ist the
subject number.
Is this possible to do in Python? Or is
there an easier way to do it? | have
tried a number of different batches of
code, but none of them get the job
done, For example,
#l/usr/bin/python
import sys, string, glob, os
fis = glob.glob(e*/Users/slevclab/Desktop/Acceptability Judgenent Task/data/**);
outfile = open(*suemary. 10g", "w');
for x in fis:
File-open(x,‘r'):
data-file.read();
File.close();
outfile.write(data);
ourfile.close();
Gives me the error,
Traceback (sost recent call last)
File “fileconcat.py", line 8, in
Fleropen(x,"r");
YoError: [Ereno 21] Is 2 directory
acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Term:
1ck Overflow Network, is subject to these policies and terms
f Stack Overfiow's Products and Services, including th
hitpsstackovortow.comiquestions/6831780!pythan-fle-concatenaton7p5R0%18 Pytnon Fle Concatenation- Stack Overtow
fron glob inport igiob
import shutil
import os
PATH = r'/Users/slevelab/Desktop/Acceptability Judgement Task/data/*"
destination = open("sunmary..og', ‘xo")
for filenane in igleb(os.path.join(PATH, "*.10g"))
shut iL.copyfileobj(open(filenane, ‘rb'), destination)
destination.close()
This gives me an “invalid syntax"
error at the "for filename’ line, but I'm
not sure what to change.
python file lagging concatenation
‘asked Jul 26°11 at 14:38
534] Ryan Simmons
fib 416 2 14 25
4 Answers
The syntax is not related to the use of
glob. You forget the "* at the end of
the for statement:
for filenane in iglob(os.path. join(PAY
But the following pattern works :
PATH « r'/Users/slevelab/Desktop/Accey
destination = open('sunmary..og', "xo
for filenane in iglob(PATH):
shut L-copyileobj (open(filenane,
destination.close()
‘answered Jul 2
B vce
Bazs1 10 16
attaet
1 relevant ads and job listings, By using our sit
acknowledge that you have read and understand our and our Your use
f Stack Overfiow's Products and Services, including the
1ck Overflow Network, is subject to these policies and terms
htpsstackovertow.comiquestions/6831780!pyihan-fle-concatenaton 2a7p5R0%18 Pytnon Fle Concatenation- Stack Overtow
you are not using sys, string of
os , 80 there is no need to import
those
fis = glob.glob(r'/Users/slevclab/Dest
here, you are selecting the subject
folders. Since you are interested in
sunnary.t0¢. files within these folders,
you may change the pattern as,
follows:
is = glob. glob('/users/slevelab/beskt
Task/data/*/suemary 10g")
In Python, there is no need to end
lines with semicolons.
utile = open(susmary. 10g", "w')
for x in fis:
file = open(x, 'r")
data = file.reac()
file. close()
outfile.write(data)
ourfile.close()
answered Jul 26 "1% at 1447
Ferdinand Beyer
Mok 12 108 124
‘The colon ( : )is missing in the for
line. Besides you should use with
because it handles closing the file
(your code is not exception safe)
‘from glob import iglob
import shutil
import os
pan
P'/Users/slevclab/desktop/Accey
with open(sunmary.1og", "wo") as dest
for filenase in iglob(os.path.joir
vith open(filenane, ro") as 5
shutil .copyfileoby(in_, de
answered Jul 26""% at 1447
GH Michael Markert
ete 370 2 14 18
acknowledge that you have read and understand our
verllow’s Products and S
htpsstackovertow.comiquestions/6831780!pythan-fle-concatenaton
1 relevant ads and job listings. By using our site, y
1ck Overflow Netw
and our
bie
to these policies and
a6