![]() ![]() |
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:06 AM
Post
#2
|
|
|
Group: Members Posts: 1,213 Joined: 9-June 05 From: Gobi Desert, Mongolia Member No.: 7,306 |
ok got the generator to work properly, I can randonly generate any pic from a specific directory and change the launcher.conf file.
Problem: How to tell the launcher to "refresh" the wallpaper. Or is there a commandline to set the wallpaper already, so I dont have to edit the launcher.config file? BTW: in that file containes a list of fastload apps too, along with some other interesting stuff. |
|
|
|
Aug 10 2005, 11:15 AM
Post
#3
|
|
|
Group: Members Posts: 668 Joined: 3-December 03 From: US Member No.: 1,034 |
So you got the string replace command in Python ?
If you didnt .... You can search each line till eof after reading it into a list (or tuple, I keep getting confused which is which) readFile = open(dataPath + "launcher.conf") readLines = readFile.readlines() for line in readLines : header = line.split("=")[0] entry = line.split("=")[1] Once you find a match (on header or enrty), use replace function to replace a string with new string For reference - string functions of Python http://docs.python.org/lib/string-methods.html |
|
|
|
Aug 10 2005, 11:59 AM
Post
#4
|
|
|
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() |
|
|
|
Aug 10 2005, 06:56 PM
Post
#5
|
|
![]() Group: Members Posts: 329 Joined: 1-July 04 Member No.: 3,880 |
Regular expressions are your friend
CODE for line in fileinput.input( fileToSearch ): fileToOutput.write(re.sub(r'wallFName =.*', "wallFName = " + wallpic, line)) And yes, a compiled re would be faster but I'm too lazy to type the extra line of code |
|
|
|
Aug 11 2005, 12:08 AM
Post
#6
|
|
|
Group: Members Posts: 1,213 Joined: 9-June 05 From: Gobi Desert, Mongolia Member No.: 7,306 |
cool, gotta try that one, you recommend any books? I am a vb programmer as well, with some fortran.
|
|
|
|
Aug 11 2005, 12:27 AM
Post
#7
|
|
|
Group: Members Posts: 385 Joined: 3-December 03 Member No.: 1,038 |
For having the launcher reloaded, serach the forums for qcop; sending a qcop message will tell qtopia to reload its settings.
HTH |
|
|
|
Aug 11 2005, 03:44 AM
Post
#8
|
|
![]() Group: Members Posts: 329 Joined: 1-July 04 Member No.: 3,880 |
QUOTE(bam @ Aug 11 2005, 03:08 AM) Truth be told, I learned Python just by reading the Tutorial and the Library Reference but I came in with a lot of experience in other languages. If you're an experienced programmer, you may find Dive Into Python helpful. It's probably a good place to start since you can read it online and then if it looks too advanced save your $$$ for something a little more beginner oriented. If you're looking for something a little more introductory I'm told that the O'Reilly Learning Python book is pretty good. |
|
|
|
Aug 11 2005, 08:08 PM
Post
#9
|
|
|
Group: Members Posts: 1,213 Joined: 9-June 05 From: Gobi Desert, Mongolia Member No.: 7,306 |
|
|
|
|
Aug 16 2005, 02:33 AM
Post
#10
|
|
|
Group: Members Posts: 385 Joined: 3-December 03 Member No.: 1,038 |
You can take a look at
http://www.oesf.org/index.php?title=Sharp_...a_QCop_Messages Maybe the QPE/System restart() would do the trick for you. |
|
|
|
Aug 16 2005, 09:58 AM
Post
#11
|
|
|
Group: Members Posts: 50 Joined: 10-November 03 Member No.: 832 |
QUOTE(kopsis @ Aug 11 2005, 03:44 AM) QUOTE(bam @ Aug 11 2005, 03:08 AM) Truth be told, I learned Python just by reading the Tutorial and the Library Reference but I came in with a lot of experience in other languages. If you're an experienced programmer, you may find Dive Into Python helpful. It's probably a good place to start since you can read it online and then if it looks too advanced save your $$$ for something a little more beginner oriented. If you're looking for something a little more introductory I'm told that the O'Reilly Learning Python book is pretty good. Other resouces include: A Byte of Python (http://www.byteofpython.info) and the The Python Grimoire (http://the.taoofmac.com/space/Python/Grimoire) |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 21st May 2013 - 10:59 PM |