Hi ,
i am in the middle of creating the script to match the string and replace it
in mount.txt
mickey:/work1 /work1 bla bla bla
mickey:/work2 /work2 bla bla bla
micket:/job /job bla bla bla -
#!/usr/bin/python
-
import string
-
s = open("/usr2/py/mount.txt","r+")
-
for line in s.readlines():
-
print line
-
string.replace(line, 'mickey','minnie')
-
print line
-
s.close()
-
However, the string.replace seems not working, any advice??? Thank you!
I am using CentOS 4.6 and the python is 2.3.4
7 164264 bvdet 2,851
Expert Mod 2GB
Hello DannyMc, string.replace() does not modify the argument string in place, but returns a modified string. You must make an assignment. Unless your file is very large, I would suggest updating the text in the file by reading the file, modifying the text, and writing the modified text back to the file. You can use string method replace() instead of importing the string module. - s = open("mount.txt").read()
-
s = s.replace('mickey', 'minnie')
-
f = open("mount.txt", 'w')
-
f.write(s)
-
f.close()
thanks for the solution bvdet!!
you can use fileinput for inplace editing -
import fileinput
-
for line in fileinput.FileInput("file", inplace=1):
-
line=line.replace("old","new")
-
print line
-
Hi guys, i managed to solve my problem with the following scripts: -
#!/usr/bin/python
-
import re
-
#fstab location, use w mode as need to overwrite whole file.
-
s = open("/usr2/py/mount.txt","r")
-
#temp txt file to store new mount.
-
tmpfile = open("/usr2/py/tmp.txt","a")
-
#new nas mount point
-
newmount = open("/usr2/py/newmount.txt","r")
-
#search pg-swnas1 line
-
for line in s.readlines():
-
if re.search("filer", line, re.IGNORECASE) != None:
-
print line
-
else:
-
tmpfile.write(line)
-
#read the latest mount point
-
readmount = newmount.read()
-
#append to temp file
-
tmpfile.write(readmount)
-
s.close()
-
tmpfile.close()
-
tmpfile = open("/usr2/py/tmp.txt","r")
-
readtmp = tmpfile.read()
-
s = open("/usr2/py/mount.txt","w")
-
s.write(readtmp)
-
-
tmpfile.close()
-
newmount.close()
-
-
Though the code works, but it seems not a clean code. Is there anyway to simplify it???
bvdet 2,851
Expert Mod 2GB
I am a bit unclear about what mount.txt and newmount.txt contain and what you need to replace. You do not need re. You can test for membership with the in operator. If I understand correctly, this should work (untested): - mountList = open("/usr2/py/mount.txt", "r").readlines()
-
newmountList = open("/usr2/py/newmount.txt","r").readlines()
-
outputList = [item for item in mountList if "filer" not in item.lower()]
-
outputList.extend(newmountList)
-
f = open("/usr2/py/mount.txt","w")
-
f.write(''.join(outputList))
-
f.close()
hi bvdet,
The script works fine for single file but how can i replace for multiple files in a directory .
could you write the script please .
My file extensions are .xlf
folder c:/mrit/
with kind regards,
MJ
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Julie |
last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB)
for a given string.
The files are unindexed and unsorted, and for the purposes of my immediate
requirements, can't...
|
by: thorpk |
last post by:
I have an access database that i have added a pop up calendar to, the
Table information for the Date Reported field is
Date/Time
format short date,
input mask is 00/00/0000.
i have created a...
|
by: fh217 |
last post by:
Hi,
Is there a way in Access to retrieve data in a specific area of a text file?
The data ( a 6 character date in YYMMDD format ) always resides on the
second line in positions 11 to 16. Thank...
|
by: ogo796 |
last post by:
Hi everyone i want to search the text file,using pregmatch() but it seems like
it dose't serch.maybe my serching pattern is not right.look at the following code.
Myfile look like this...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
| |