#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Importing 50 txt files python


    Hi

    I know how to import a individual file

    def cvt(s,default=-1):
    try:
    return float(s)
    except ValueError:
    return default

    import csv
    with open("Dealer1.txt", "rb") as csvfile:
    reader = csv.reader(csvfile, delimiter="\t")
    data = list(reader)

    I have a folder that contains 50+ files, using Glob, I created the following code
    import os
    import glob

    os.chdir('c:/Performance')
    files = glob.glob('*.txt')
    print files

    Now is there a way that instead of me importing individual files I can actual import all 50 files data at the same time, all 50 have the same layout can I import all the data from the txt files in one go

    Jezza
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Use a loop.

    for name in glob.glob('*.txt'):
    work_with_file(name)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Use a loop.

    for name in glob.glob('*.txt'):
    work_with_file(name)
    I am unsure what you are saying as I have entered the details and it errors at ''work_with_file"

    If you can assist with where the details go that would help:
    The folder is located at c:/Supplier Performance

    The name of the files are: 'Dealer 1','Dealer 2','Used 1','Used 8' etc.

    There is currently50 files but it can get 10 added per week and they will have different names.

    Jezza
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    You must supply function work_with_file .

    Please study the python tutorial.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo