Aug 9 2005, 11:35 AM
Post
#1
|
|
|
Group: Members Posts: 1,213 Joined: 9-June 05 From: Gobi Desert, Mongolia Member No.: 7,306 |
I need to search a line of text out in a file and replace it, so far the code examples on the net have produced empty files.
Basically the file is a *.conf file, now the structure of the file looks alot like a win *.ini file and in visual basic there is a function to access the sections (the headers in brackets) and each entry (the name with '='). Now the question, does python support or rather have a built in function for handleing that? If yes what is it? If no then how do I do a search and replace? I only have a partial string and want to replace the whole line until the eof(its the last line in the file) and second question how is the Launcher.conf 'reloaded' after a change, does it get checked every so often? or even better, what command line will change my wallpaper? Obviously you have the direction I am looking in so far. |
|
|
|
![]() |
Aug 10 2005, 11:59 AM
Post
#2
|
|
|
Group: Members Posts: 1,213 Joined: 9-June 05 From: Gobi Desert, Mongolia Member No.: 7,306 |
yea, go that, actually I only know the entry name "wallFName =" the rest is unknown, so I used the "for line in sFile" then did a if/then/else construct when it finds the line re-write the entire line(this is what *.replace does not do"
hek I will post the code, I just have to find a way to tell the launcher o refresh the wallpaper, google provided nothing CODE #!/usr/bin/env python
import os import sys import fileinput import glob import random import re piclist=glob.glob('/hdd3/Documents/Image_Files/wallpapers/*.*') wallpic=random.choice(piclist) wppath="/hdd3/Documents/Image_Files/wallpapers/" + wallpic #/home/root/settings/launcher.conf textToSearchFor= "wallFName" newWallP= "wallFName =" + wppath fileToSearch= "/hdd3/Documents/temp/launcher.conf" fileToOutput= "/hdd3/Documents/temp/tempfile.conf" fileToOutput = open( fileToOutput, 'w' ) #print "outfile = ", fileToOutput for line in fileinput.input( fileToSearch ): #fileToOutput.write( line.replace( textToSearchFor, textToReplaceWith ) ) slinee="" sline=line[0:9] if sline == "wallFName": fileToOutput.write(newWallP) else: fileToOutput.write(line) fileinput.close() fileToOutput.close() |
|
|
|
bam Python Question Aug 9 2005, 11:35 AM
bam ok got the generator to work properly, I can rando... Aug 10 2005, 11:06 AM
nilch So you got the string replace command in Python ?
... Aug 10 2005, 11:15 AM
kopsis Regular expressions are your friend
CODEfor line ... Aug 10 2005, 06:56 PM
bam cool, gotta try that one, you recommend any books?... Aug 11 2005, 12:08 AM
kopsis QUOTE(bam @ Aug 11 2005, 03:08 AM)cool, gotta... Aug 11 2005, 03:44 AM
slapout QUOTE(kopsis @ Aug 11 2005, 03:44 AM)QUOTE(ba... Aug 16 2005, 09:58 AM
zmiq2 For having the launcher reloaded, serach the forum... Aug 11 2005, 12:27 AM
bam QUOTE(zmiq2 @ Aug 11 2005, 12:27 AM)For havin... Aug 11 2005, 08:08 PM
zmiq2 You can take a look at
http://www.oesf.org/index.... Aug 16 2005, 02:33 AM![]() ![]() |
|
Lo-Fi Version | Time is now: 20th May 2013 - 11:41 PM |