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

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Writing contents of globbed file


    Does anyone have ready code to execute the following steps:
    1. glob a set of files (e.g., *.csv)
    2. ask for user input to create a file name
    3. open a new file with the user-generated name (#2)
    4. write the contents of the globbed files into the new file (not the names of the files that were globbed, but the actual contents, e.g., the csv data)

    I can get 2 of 4 steps

    Many Thanks!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Since, generally, you won't know whether the files are text only, I'd open both the new file and the other files as binary.
    Code:
    import glob
    outfile=open (<new file name>, 'wb')
    lstFiles=glob.glob(path+"*.*")
    for n in lstFiles:
        f=open(n, 'rb')
        outfile.write(f.read())
        f.close()
    outfile.close()
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    thanks!


    That did it. I had the ordering of events wrong, and the embedded .write(f.read()) was they key. Also very concisely written.




    Originally Posted by rrashkin
    Since, generally, you won't know whether the files are text only, I'd open both the new file and the other files as binary.
    Code:
    import glob
    outfile=open (<new file name>, 'wb')
    lstFiles=glob.glob(path+"*.*")
    for n in lstFiles:
        f=open(n, 'rb')
        outfile.write(f.read())
        f.close()
    outfile.close()

IMN logo majestic logo threadwatch logo seochat tools logo