By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

Reading specific lines from file and writing them another file

P: 2
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!

f=open("/home/flv1/histograms/gauss_fit_*.dat",'r')
f2=open("/home/flv1/test2.txt",'w')
for line in f:
f2.write(line)
f.close()
f2.close()
Jul 25 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 849
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
  2.  
  3. filelist = os.listdir(os.curdir)
  4. files = [file for file in filelist if file.endswith('.txt') and file.startswith('log_')
  5.  
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

jlm699
100+
P: 314
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
  2.  
  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()
  13.  
HTH!
Jul 25 '08 #3

Post your reply

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