[Tutor] Editing files through Python
Bryan
juicebypi216 at yahoo.com
Wed Sep 1 20:05:44 CEST 2004
Hi Bob,
I've been playing around with the advice you was sent me and have become a little confused about the ideas of the input_file (r) and the output_file (w). Can they be the same file? Do I have to close the input file before writing to the output file? Does this example show what always has to be done when using input and output files?
Here is the program I wrote following your example, and an error that always appears, relating to the input file, that is perplexing me. I think it probably has a simple solution, but I just don't understand enough about input and output files to figure it out. Is there anything in the Python tutorial or elsewhere that I can read more about reading and writing to input and output files.
Here is the program:
input_file = file('C:\BryFolder\coherence.dat', 'r')
lines = input_file.readlines()
output_file = file('C:\BryFolder\coherence.dat', 'w')
for line in lines:
nums = line.split(" ")
retain = nums[:5]
new_line = ", ".join(retain)+"\n"
output_file.write(new_line)
output_file.close()
Here is the error:
Traceback (most recent call last):
File "C:\BryFolder\cohprog3.py", line 1, in ?
input_file = file('C:\BryFolder\kt.1.24.data.036rd.test.dat', 'r') # substitute your path to the file
IOError: [Errno 2] No such file or directory: 'C:\\BryFolder\\kt.1.24.data.036rd.test.dat'
Thanks for the help
Bryan
Bob Gailer <bgailer at alum.rpi.edu> wrote:At 01:38 PM 8/30/2004, Bryan wrote:
>
>I am a beginner in Python, and this is my first time using this list so I
>hope my question is acceptable.
We are here to help, but not just give answers. However your request seems
to me to merit a program, which I hope you will study to enhance your
understanding of Python. Please ask more questions.,
>Basically, my goal is to read data files that are already on my hard drive
>into Python, and then edit them, deleting unneccesary portions of data.
>
>I have already figured out how to read the files into Python, I just
>cannot figure out how to edit them. Here are the difficulties:
>
>How do I delete, cut, or paste portions of the data I am reading and how
>do I tell the program to jump to that specific portion of data that I want
>to alter?
The usual approach is to read the file into a Python variable, process the
variable and write a new file (which can have the same name as the input
file, rather than to attempt to manipulate the file itself. It is a LOT
easier this way.
> Here is a somewhat complicated example, similar to what I want to do -
>
>I have two rows of numbers, as shown below. Each row has 10 numbers
>(hopefully they will show up as two rows of numbers in the email (1-10 and
>11-20).
>
>1, 2, 3, 4, 5, 6, 7, 8, 9, 10
>11, 12, 13, 14, 15, 16, 17, 18, 19, 20
>
>What commands might I use to automatically go through each row, deleting
>the 6th through 10th numbers in each row (6-10 in the first row and 16-20
>in the second row).
I have written a complete program, just to avoid any ambiguities.
input_file = file('num.txt', 'r') # substitute your path to the file
lines = input_file.readlines() # list of lines
output_file = file('num.txt', 'w') # substitute your path to the file
for line in lines:
nums = line.split(", ") # assumes numbers separated by ", ", gives list
of numbers
retain = nums[:5] # keep first 5 numbers
new_line = ", ".join(retain)+"\n" # reassemble into a line
output_file.write(new_line) # write to file
output_file.close()
Please note this depends on consistent number separators. If the separators
vary, then the approach gets more complex.
Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell
---------------------------------
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040901/5463e69a/attachment.htm
More information about the Tutor
mailing list