469,921 Members | 2,183 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,921 developers. It's quick & easy.

Reading specific lines from file and writing them another file

Hello all, I am very new to programming and I have recently started using python. I am interested in creating a file to essentially compare data points.

I am curious if it is possible to read specific lines from files and then write those lines to another file in separate columns (for comparative purposes)? If so could someone show me how I can alter my code to do this?

Below is the code that I am using and wish to alter. The files that I am interested in reading all begin with gauss_fit_ and end with .dat , however when I ran this program to read all these files ( instead of one) it did not work.

Thanks A Million In Advance!

for line in f:
Jul 25 '08 #1
2 2885
849 Expert 512MB
As an example, to get all the files that end in .txt and start with log_ in the current directory, use

Expand|Select|Wrap|Line Numbers
  1. import os
  3. filelist = os.listdir(os.curdir)
  4. files = [file for file in filelist if file.endswith('.txt') and file.startswith('log_')
You could also do this with regular expressions, but I think that's overkill (we're not using Perl, after all).

Open doesn't support shell wildcards like *, I'm afraid. You'll have to open each file separately (or just iterate through a list of files and open each one). I hope you can extrapolate from here.
Jul 25 '08 #2
314 100+
You should read the posting guidelines to learn about [code] tags, as they increase the readability of your posts, thereby making it easier for the other forum members to answer your question!

Unfortunately, Python's open() call is not intuitive enough to look at a wildcard the same way that the command line does. However by using the os module there are some ways around that. I'm not sure exactly how you want your program to work in terms of number of input files but here is an example to get you going:
Expand|Select|Wrap|Line Numbers
  1. import os
  3. fout = open( "/home/flv1/test2.txt", 'w' )
  4. my_dir = '/home/flv1/histograms'
  5. dir_list = os.listdir( my_dir )  # This is like an 'ls' command
  6. for file_name in dir_list:
  7.     if 'gauss_fit_' in file_name and file_name.endswith( '.dat' ):
  8.         fh = open( os.path.join( my_dir, file_name ), 'r' )
  9.         for line in fh:
  10.             fout.write( line )
  11.         fh.close()
  12. fout.close()
Jul 25 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by Yi Xing | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.