473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 3079
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 "y.py" thanx -python newbie-
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 fast way of doing this in Python? Thanks. Yi Xing
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 allocation??? Do i need to use some interrupt calls in dos??
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
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 display with $filename="text.txt"; //open file...
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 can see logs for different day in one file only. ...
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 dont want to read through all lines, I just want to...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.