473,473 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

writing lines from multiple text files into one text file

1 New Member
I am new to python, and coding in general. Looking at the code below, why is my master file writing the lines from each text file twice?

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import glob
  3. FileName = "master.txt"
  4. fout = open(FileName, "w")
  5. for filename in glob.glob("*txt"):
  6.     fin = open(filename, "r")
  7.     lines = fin.readlines()
  8.     fin.close()
  9.     for lidx in lines:
  10.         fout.write( "%s" % lidx )
  11. fout.close()


The combined file I am getting looks like this:
text from file1
text from file2
text from file3
text from file1
text from file2
text from file3

Any help is appreciated.
Dec 11 '11 #1
2 2870
PythonInspired
1 New Member
This code works exactly as you want it to when I test it.

By chance, do you have any links to these files in the same directory? I know that sounds odd, but when I create links to any of the files I tested this code with, the contents of the linked file were doubled in the output. (the glob section of the python documentation explains this).

I hope this is of some use, because otherwise your code works perfectly for me.

Anything more than this and I am absolutely useless. I am a beginner and decided that the best way to improve is to keep looking at code and trying to figure things out as I go.
Dec 12 '11 #2
bvdet
2,851 Recognized Expert Moderator Specialist
I don't see anything wrong with your code either. I have a suggestion for improving your code. Instead of
Expand|Select|Wrap|Line Numbers
  1.     for lidx in lines:
  2.         fout.write( "%s" % lidx )
I prefer
Expand|Select|Wrap|Line Numbers
  1.     fout.write("".join(lines))
Dec 12 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Lorn Davies | last post by:
Hi there, I'm a Python newbie hoping for some direction in working with text files that range from 100MB to 1G in size. Basically certain rows, sorted by the first (primary) field maybe second...
2
by: caro | last post by:
Hi I am trying to write two Select * statements to the same text file using bcp (from a stored procedure). But cannot find a way of appending to a file using bcp. Does anyone know if this...
11
by: MM | last post by:
Hi I have never written any C programs before, but it seems that I need to do so now. Hope some of you out there can spend a few minutes and help me by writing a simple example of something...
6
by: Holger Kasten | last post by:
Hello, I am looking for a fast and efficient way to compare two text files and create a thrid one. E.g. Input file 1: Number 1 Number 2
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
2
by: Chris Murphy via DotNetMonster.com | last post by:
Hey all, just wondering if anyone can point me in the right direction. I'm developing a solution that allows a user to store multiple text-based content (like code snippets, notes, documents etc.)...
3
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
7
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following...
3
by: TonyJ | last post by:
Hello! I supposed to modify some text files. In the file(s) which consist of different section. In one section you might have Path = <Installation_path>. The program will then replace the text...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.