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

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Help combining files


    Hello,

    I am trying to take all the files in my folder proteinsequences and combine them all into one file. I have been on the nternet trying to find other peoples programs. This one seems like it should work but I get a syntax error. The arrow points to the =. in "print(l, file = out)" Any suggestions?

    #set to the file extension of "to-be-merged" files
    ext='._ALL'
    #set to your working directory
    dir_path='/home/bonita/Documents/proteinsequences'
    #set to the name of your output file
    results='NB_protein.fasta'

    import os
    files=os.listdir(dir_path)
    for f in files:
    if f.endswith(ext):
    data = open(f)
    out = open(results, 'w')
    for l in data:
    print(l, file = out)
    data.close()
    out.close()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,700
    Rep Power
    480
    Sure,
    the code you posted is a python3 code. It won't work in python2.

    Hmm, it has recently been shown to me that you might be able to import print_function from the future.

    from __future__ import print_function


    But why are you writing a program?

    in unix, and it looks like you've got a unixy system, the command is:

    $ cat *._ALL > NB_protein.fasta

    I'm not going to bother checking what the DOS command is, but it's probably

    A:> copy *._ALL NB_protein.fasta
    [insert new floppy for A:]
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    3
    b49P23TIvg is right, no point writing a program that is already done by dos or terminal

    but to answer your question your problem is you print(l, file = out), this doesn't work this way at least not to my knowledge use something like the following
    Code:
    file = open('INFORMATION.txt', 'w')
    a_file = open('MORE_INFORMATION.txt', 'w')
    results = open('RESULTS.txt', 'w')
    
    results.writeline("%s\n" % for item in file)
    
    results.writeline("%s\n" % for item in a_file)
    
    file.close()
    a_file.close()
    results.close()
    That should be good enough to give you an idea of what you need to do. hope this has helped good luck with python
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Cool Thanks!


    Thank you for the help guys.

    You were right I just used the cat *>file command in the terminal. The only reason I was trying to code it is because I didn't know it was so easily done.


    Thank you again for the help!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    33
    Rep Power
    3
    no problem

IMN logo majestic logo threadwatch logo seochat tools logo