Load all csv/txt files from one directory and merge them via python

AEA

I have a folder which contains hundreds (possibly over 1 k) of csv data files, of chronological data. Ideally this data would be in one csv, so that I can analyse it all in one go. What I would like to know is, is there a way to append all the files to one another using python.

My files exist in folder locations like so:

C:\Users\folder\Database Files\1st September
C:\Users\folder\Database Files\1st October
C:\Users\folder\Database Files\1st November
C:\Users\folder\Database Files\1st December
etc

Inside each of the folders there is 3 csv (I am using the term csv loosly since these files are actually saved as .txt files containing values seperated by pipes |)

Lets say these files are called:

MonthNamOne.txt
MonthNamTwo.txt
MonthNameOneTwoMurged.txt

How would I, or even is it possible to code something to go through all of these folders in this directory and then merge together all the OneTwoMurged.txt files?

Dennis Sylvian

For all files in folder with .csv suffix

import glob
import os

filelist = []

os.chdir("folderwithcsvs/")
for counter, files in enumerate(glob.glob("*.csv")):
    filelist.append(files)
    print "do stuff with file:", files, counter

print filelist

for fileitem in filelist:
    print fileitem

Obviously the "do stuff part" depends on what you want done with the files, this is looking getting your list of files.

If you want to do something with the files on a monthly basis then you could use datetime and create possible months, same for days or yearly data.

For example, for monthly files with the names Month Year.csv it would look for each file.

import subprocess, datetime, os

start_year, start_month = "2001", "January"

current_month = datetime.date.today().replace(day=1)
possible_month = datetime.datetime.strptime('%s %s' % (start_month, start_year), '%B %Y').date()
while possible_month <= current_month:
    csv_filename = possible_month.strftime('%B %Y') + '.csv'
    month = possible_month.strftime('%B %Y').split(" ")[0]
    year = possible_month.strftime('%B %Y').split(" ")[1]
    if os.path.exists("folder/" + csv_filename):
        print csv_filename
    possible_month = (possible_month + datetime.timedelta(days=31)).replace(day=1)

Obviously you can change that to however you feel fit, let me know if you need more or if this suffices.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to merge all files that end with .mk in a directory and subdirectories into one file?

分類Dev

Reading all files from a directory

分類Dev

How to delete all files and directory except one named directory from a specific folder in centos

分類Dev

How use minimum number of commands to copy all .txt files from all subdirectories to one directory?

分類Dev

Is there a way to somehow group several symbols from different files together so that if one of them is referenced, all are linked?

分類Dev

Reading only .txt files from an input directory, then getc all contents into one array in C

分類Dev

How to move all files (excluding sub-directories) from one directory to another?

分類Dev

copying files from one directory to another

分類Dev

Recursively copy files from one directory to another

分類Dev

Add all bib files from a directory to bookdown

分類Dev

Finding files from list and copying them into new directory

分類Dev

Python - Renaming all files in a directory using a loop

分類Dev

Moving files from one directory to another directory in HDFS using Pyspark

分類Dev

How to move all files from current directory to upper directory?

分類Dev

Makefile process all files in one directory, output to another.

分類Dev

How can i get all files on disk with a specific extension using 'Directory.getFiles' and save them in a list

分類Dev

How to find all file owned by one user and copy them to another directory in RHEL 8?

分類Dev

Zip all files in directory?

分類Dev

How to merge all csv files in a specific folder using python and os

分類Dev

How to refer to multiple files from a directory to one command

分類Dev

How do I remove all files from wtihin a certain directory except for a child directory of that directory?

分類Dev

How to get all files from a directory in Azure BLOB

分類Dev

Swift delete all files from particular Document Directory Location

分類Dev

Select all files from directory that contain file with given name

分類Dev

How to clear data from all files present in a directory?

分類Dev

Search all docx files with python-docx in a directory (batch)

分類Dev

Python script to compress all pdf files in a directory on Windows 7

分類Dev

Get all the nested arrays from main array and merge each of them according to keys Knockout.Js

分類Dev

Python generating different files from one file

Related 関連記事

  1. 1

    How to merge all files that end with .mk in a directory and subdirectories into one file?

  2. 2

    Reading all files from a directory

  3. 3

    How to delete all files and directory except one named directory from a specific folder in centos

  4. 4

    How use minimum number of commands to copy all .txt files from all subdirectories to one directory?

  5. 5

    Is there a way to somehow group several symbols from different files together so that if one of them is referenced, all are linked?

  6. 6

    Reading only .txt files from an input directory, then getc all contents into one array in C

  7. 7

    How to move all files (excluding sub-directories) from one directory to another?

  8. 8

    copying files from one directory to another

  9. 9

    Recursively copy files from one directory to another

  10. 10

    Add all bib files from a directory to bookdown

  11. 11

    Finding files from list and copying them into new directory

  12. 12

    Python - Renaming all files in a directory using a loop

  13. 13

    Moving files from one directory to another directory in HDFS using Pyspark

  14. 14

    How to move all files from current directory to upper directory?

  15. 15

    Makefile process all files in one directory, output to another.

  16. 16

    How can i get all files on disk with a specific extension using 'Directory.getFiles' and save them in a list

  17. 17

    How to find all file owned by one user and copy them to another directory in RHEL 8?

  18. 18

    Zip all files in directory?

  19. 19

    How to merge all csv files in a specific folder using python and os

  20. 20

    How to refer to multiple files from a directory to one command

  21. 21

    How do I remove all files from wtihin a certain directory except for a child directory of that directory?

  22. 22

    How to get all files from a directory in Azure BLOB

  23. 23

    Swift delete all files from particular Document Directory Location

  24. 24

    Select all files from directory that contain file with given name

  25. 25

    How to clear data from all files present in a directory?

  26. 26

    Search all docx files with python-docx in a directory (batch)

  27. 27

    Python script to compress all pdf files in a directory on Windows 7

  28. 28

    Get all the nested arrays from main array and merge each of them according to keys Knockout.Js

  29. 29

    Python generating different files from one file

ホットタグ

アーカイブ