472,353 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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!

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
2 3018
Laharl
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
  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
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
  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

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

Similar topics

2
by: se7en | last post by:
okay, i cant seem to run a file with another file as an argument. e.g I want to send the file "x.met" as an argument when running the file...
5
by: rashmi | last post by:
how to use static function defined in one file in another file is that impposiible in 'c '
12
by: Yi Xing | last post by:
Hi All, I want to read specific lines of a huge txt file (I know the line #). Each line might have different sizes. Is there a convenient and...
4
by: system55 | last post by:
I need some help.... How can i create a program wherein it can copy a file about 2MB in size, into another file using the extended memory...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename =...
1
by: KennedyR | last post by:
Basically what I want to do is just display lines 10 - 20 for example. Nothing else. Is there a way to do this? I can get the whole document to...
3
by: maheshkadam | last post by:
Hi friends I am new to perl so please guide me. I have one application which created backup log file every day.But it appends that file so you...
13
by: gobblegob | last post by:
Hi, I would like to know how i could read and write in a specific line in a text file with VB6. eg. first line second line third line I...
1
by: ram patidar | last post by:
how i can copy the contents of one file to another file? a block at a time usng win32 API? ram patidar
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.