Close Menu
    Facebook X (Twitter) Instagram
    Trending
    • SharePoint Internal Column Name Encoded Character List
    • Satchel Pelts List for Red Dead Redemption 2
    • Create a Video Wall With FFmpeg
    • Mitsubishi Outlander Towbar Electrics
    • Mitsubishi Outlander Towbar Installation
    • Telephone Block List
    • Hard Drive Repair After Wrong Voltage Mishap
    • How To Send Excel Workbook As Email Using Button
    YouTube Instagram Facebook RSS
    Technology Spy
    • Tutorials & Help
    • Reviews
    Technology Spy
    You are at:Home»Development»Python»How To Combine Text And CSV Files With Python
    Coding Programming Guides

    How To Combine Text And CSV Files With Python

    0
    By Matt on June 21, 2013 Python, Tutorials & Help

    Following on from my How To Combine Text And CSV Files With Python article I decided to create a script to combine CSV or Text files into one file. This isn’t something I need to do on a regular basis but when I do automating it saves a lot of time.

    The script is fairly basic. It opens each file within a specified directory, reads the content and appends it to a specified output file.

    Although I am usually dealing with CSV files this technique works just fine with plain text files.

    #!/usr/bin/env python
    # Import module
    import os
    
    # Define output filename
    OutputFilename = 'combined.csv'
    
    # Define path to input and output files
    InputPath  = 'D:/MyData'
    OutputPath = 'D:'
    
    # Convert forward/backward slashes
    InputPath  = os.path.normpath(InputPath)
    OutputPath = os.path.normpath(OutputPath)
    
    # Define output file and open for writing
    filename = os.path.join(OutputPath,OutputFilename)
    file_out = open(filename, 'w')
    print "Output file opened"
    
    # Loop through each file in input directory
    for file in os.listdir(InputPath):
      # Define full filename
      filename = os.path.join(InputPath,file)
      if os.path.isfile(filename):
        print "  Adding :" + file
        file_in = open(filename, 'r')
        content = file_in.read()
        file_out.write(content)
        file_in.close()
    
    # Close output file
    file_out.close()
    print "Output file closed"
    

    The script uses the os module to provide file and directory manipulation functions. This makes opening, writing and closing files nice and easy.

    The only modifications you will need to make are to the OutputFilename, OutputPath and InputPath variables. The “os.path.normpath” and “os.path.join” should mean you don’t need to worry about forward and backward slashes in your paths.

    This example reads the entire input file and adds the whole content to the output file. It should be possible to modify this example to read the input file line by line and selectively add lines to the output based on some logic.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow To Send HTML Emails In Python
    Next Article How To List Files In Python Filtered By Age And Size

    Related Posts

    Create a Video Wall With FFmpeg

    Mitsubishi Outlander Towbar Electrics

    Mitsubishi Outlander Towbar Installation

    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    109

    Recent Posts
    May 1, 2025

    SharePoint Internal Column Name Encoded Character List

    March 24, 2025

    Satchel Pelts List for Red Dead Redemption 2

    January 29, 2025

    Create a Video Wall With FFmpeg

    Categories
    • 3D Printing
    • Android
    • Arduino
    • Development
    • ESP8266
    • Excel
    • Gaming
    • General
    • GIMP
    • Home Automation
    • JavaScript
    • Linux
    • Microsoft Office
    • Mobile Devices
    • Oracle APEX
    • Python
    • Raspberry Pi
    • Reviews
    • Security
    • SharePoint
    • Tutorials & Help
    • VBScript
    Web Tools

    A set of quick and basic online tools for web designers and software developers :

    • Hash Generator
    • Text to HTML List
    • Text to HTML Table
    • URL Encoder and Decoder
    • UNIX Timestamp Calculator
    • LED Resistor Calculator
    • Extract Email from Text
    • Mortgage Calculator
    Tags
    Android APEX Apple Arduino Black Friday Coolermaster csv Elite Dangerous email ESP-01 ESP8266 EV Excel file handling Format gaming GIMP Gritin Home Assistant Home Automation Linux lists Media os.stat os.walk Outlander Power Python Qi RDR2 Review scam Security SharePoint 2010 string Syncwire text text files TrueCrypt Ubuntu USB-C VBA VBscript Windows 10 Xbox One
    About

    Welcome to Technology Spy, a site devoted to gadgets, computers, programming and all things technology! You’ll also find product reviews for items I own as well as tutorials, guides and scripts for the software I use.

    Archives
    Other Resources
    • MattsBits
    • Raspberry Pi Spy
    YouTube Facebook Instagram Pinterest RSS

    Entries RSS | Comments RSS

    Copyright © 2025 - All Rights Reserved - Matt Hawkins

    mastodon.social@MattHawkins

    Type above and press Enter to search. Press Esc to cancel.