473,403 Members | 2,293 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,403 software developers and data experts.

Replacing a string in a text file

4
Hello Python Experts

I have a text file containing a code (written for a software)

I need to write a code in Python, to first, read the text file, search for a string, change that string with something else and save the file.

Let's say that the file name is test.txt which is in D:\ry directory, and I need to change E=100 to E=200 in this file.

I should mention that I am a beginner in python.

Thansk
May 10 '12 #1
4 1915
bvdet
2,851 Expert Mod 2GB
It could be as simple as this:
Expand|Select|Wrap|Line Numbers
  1. >>> s = open("test12.txt").read()
  2. >>> f = open("test13.txt", 'w')
  3. >>> f.write(s.replace("E=100", "E=200"))
  4. >>> f.close()
  5. >>> 
May 10 '12 #2
afshin
4
Thanks. From you code, I wrote it like this:

s = open("D:\test.txt").read()
f = open("D:\test1.txt", 'w')
f.write(s.replace("E=100", "E=200"))
f.close()

But it gives me the following error:

Traceback (most recent call last):
File "D:\test2.py", line 7, in <module>
s = open("D:\test.txt").read()
IOError: [Errno 22] invalid mode ('r') or filename: 'D:\test.txt'
May 10 '12 #3
bvdet
2,851 Expert Mod 2GB
If you use a backslash character in the file name, it's a good idea to always escape it as in "D:\\test.txt". Otherwise you could use the forward slash character "/". FYI - "\t" is interpreted as a tab character. "\\t" is interpreted as a backslash and t.
May 10 '12 #4
afshin
4
Thank you very much. As I am learning it, I will back to you with new questions soon ;)
May 10 '12 #5

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

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
0
by: Tomas Eklund | last post by:
Using: Classic ASP, File System Object, IIS Once a day my ASP script compiles some data into a csv/text file which is sent by e-mail to the customer. This file is then imported into a FileMaker...
18
by: William Payne | last post by:
Hello, I need to write a program that opens a text file and scans the entire file for a specific line and when that line is found, a particular word on that line is to be replaced. The new word is...
1
by: Joaquim Pinto | last post by:
Hi All, I'm using a richtextbox to find a particular string on a text file. Each line has more then one string separated by comas, like this: 11-11-2004, 08:30:00, 02:56:00, 04:00 I want...
31
by: microsoft.news.com | last post by:
I need to read a text file but I need to start on line 2 of the file, How can I start reading the text file starting with the second line and not the first line in the file?
31
by: J.S. | last post by:
Let's say I have a text file with parameters like the following embedded in the text: @@Textbox1@@, @@Textbox2@@, etc. Is it possible to replace the parameters in the text file with values...
2
by: Paul | last post by:
Can anyone show me a good refined way to find and replace a portion of text held in a text file held on a web server? Thanks in Advance
17
by: J.S. | last post by:
I have a text file with parameters like the following embedded in the text: @@TextBox1@@, @@TextBox2@@, etc. I know how to read this text file. However, I am trying to figure out how to...
5
by: soup_nazi | last post by:
I want to remove duplicate entries within a text file. So if I had this within a text file... Applications/Diabetic Registry/ Applications/Diabetic Registry/ Applications/Diabetic Registry/...
2
by: anii | last post by:
Hi there, I've got this piece of code, but I'm having trouble getting it to write the changes into the file. What I'm trying to do is: Read text file Find user specified character (eg. what...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.