Re: [Tutor] Working with files with python (gtk)
Hello Edward! On Thursday 15 March 2007 03:53, Edward A Robinson wrote: > I guess what Im looking for is a python command that dose what the > file command in the shell would do I don't think there is anything like this in Python's standard library. Probably Gnome has a library for identifying the file type (I've read that KDE has one). So you could ask on a Gnome mailing list for that library and use it. Offcourse you could use the 'file' program and parse the output. Regards, Eike. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files with python (gtk)
Edward A Robinson wrote: > I guess what Im looking for is a python command that dose what the > file command in the shell would do > > On 3/14/07, Edward A Robinson <[EMAIL PROTECTED]> wrote: >> This is my first time posting to the python tutor so be nice, I have >> read the mailing list and cant seem to find and answer there or on >> google so I turn to you guys for help. I'm working on a gtk >> application and I want to list all the image files in a directory. Now >> I know that if I have an image lets say foo.png and I rename that to >> foo.txt nautilus (the default gnome browser) still recognizes that >> file as an image. >> >> How can I do the same thing? As well as recognizes images I want users >> to be able to open up each image with that images default editor. So >> foo.png may open with eye of gnome but bar.jpg may open with gimp. (If >> those where the settings in nautilus) I'm a Windows person rather than a Linux one, so treat this suggestion with the contempt it deserves ;) I seem to remember that Nautilus has some kind of Python interface which perhaps could be used here to ask it what it would do with file x.y. If not, then your next best is to fall back to some system-specific mime-type lookup but I presume that would work on extensions, not on contents. Failing that, you could call the "file" system command (if that's what it's called) which presumably sniffs the header and returns something useful. Basically, knowing the file type comes down to extension or contents. Python has the mimetypes module for the former. If it's just images you're after then the Python Imaging Library (http://effbot.org/zone/pil-index.htm) seems to be able to read image types correctly, regardless of extension, so maybe that's a way to go. Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import Image >>> Image.open ("issue1.jpg").info {'jfif_version': (1, 1), 'jfif': 257, 'jfif_unit': 0, 'jfif_density': (1, 1)} >>> >>> Image.open ("issue1.xxx").info {'jfif_version': (1, 1), 'jfif': 257, 'jfif_unit': 0, 'jfif_density': (1, 1)} >>> TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files with python (gtk)
I guess what Im looking for is a python command that dose what the file command in the shell would do On 3/14/07, Edward A Robinson <[EMAIL PROTECTED]> wrote: > This is my first time posting to the python tutor so be nice, I have > read the mailing list and cant seem to find and answer there or on > google so I turn to you guys for help. I'm working on a gtk > application and I want to list all the image files in a directory. Now > I know that if I have an image lets say foo.png and I rename that to > foo.txt nautilus (the default gnome browser) still recognizes that > file as an image. > > How can I do the same thing? As well as recognizes images I want users > to be able to open up each image with that images default editor. So > foo.png may open with eye of gnome but bar.jpg may open with gimp. (If > those where the settings in nautilus) > > Thanks for your time, I hope this is an appropriate query for this mailing > list. > > -- > Edward A Robinson > -- Edward A Robinson ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Working with files with python (gtk)
This is my first time posting to the python tutor so be nice, I have read the mailing list and cant seem to find and answer there or on google so I turn to you guys for help. I'm working on a gtk application and I want to list all the image files in a directory. Now I know that if I have an image lets say foo.png and I rename that to foo.txt nautilus (the default gnome browser) still recognizes that file as an image. How can I do the same thing? As well as recognizes images I want users to be able to open up each image with that images default editor. So foo.png may open with eye of gnome but bar.jpg may open with gimp. (If those where the settings in nautilus) Thanks for your time, I hope this is an appropriate query for this mailing list. -- Edward A Robinson ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
> Alan, you missed the last part of the code - he writes the rest of > the data following the match into the file. I didn't miss it but I did misread it! :-) >file.write(contents[pos + len(name):]) I assumed 'name' was here referring to the actual value inserted. But of course that would have been a different bug to the one I described. > This is an innovative approach which may have some benefit over the > usual / read the whole file / change the data in memory / write the > whole file / method as it avoids re-writing the part of the file > before the change. Yes indeed, now that I actually see what's happening it's quite cute! > It also avoids making a new string with the modified data. True but it does involve a new string fom the slice, so I wouldn't expect a huge saving there. But the random file access should be faster than a full rewrite operation. Thanks for pointing out my mistake and my compliments to the OP! Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
Alan G wrote: > ARe you sure? The problem using seek and write is that if the data > you are inserting is bigger than your marker you will overwrite the > data following the marker. Alan, you missed the last part of the code - he writes the rest of the data following the match into the file. This is an innovative approach which may have some benefit over the usual / read the whole file / change the data in memory / write the whole file / method as it avoids re-writing the part of the file before the change. It also avoids making a new string with the modified data. These could be significant benefits if the data is very large. > This does not insert it overwrites. > You are better to insert the content into contents and > then just write it all back to the file in one move. > > >> file.write(contents[pos + len(name):]) >> file.close() This is the piece you missed. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
> > basically I took the idea and the code example given and wrote this > little function, i stuck vars in this html page like #email# and > just used it like this, " insertdata('#email#','[EMAIL PROTECTED]') > > works perfect! ARe you sure? The problem using seek and write is that if the data you are inserting is bigger than your marker you will overwrite the data following the marker. Thus if your marker were in a table like: #email#Scott's email And you overwrite the #mail# with [EMAIL PROTECTED] You will end up with: [EMAIL PROTECTED]'s email Which will not display the way you want! > def insertdata(name, data): >file = open('template.html', 'r+') >contents = file.read() >pos = contents.index(name) >file.seek(pos) >file.write(data) This does not insert it overwrites. You are better to insert the content into contents and then just write it all back to the file in one move. >file.write(contents[pos + len(name):]) >file.close() HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
Bob Gailer wrote: > At 02:55 PM 8/24/2005, Scott Oertel wrote: > >> How do I use the built in file objects to insert text into a file at a >> certain location? >> >> i.e. >> >> something, 2, chance, weee >> nothing, happened, crap, nice >> >> need to search for "something" and insert, "what," before it > > > Here's the algorithm. If you know enough Python you will be able to > code it. So put together a program, give it a try and come back with > questions. > > read the file into a string variable (assuming the file is not humungus) > find the location of "something" in the string > assemble a new string consisting of: > the original string up to the location (index) of "something" > "what" > the rest of the original string > write the new string to the file > > Bob Gailer > 303 442 2625 home > 720 938 2625 cell basically I took the idea and the code example given and wrote this little function, i stuck vars in this html page like #email# and just used it like this, " insertdata('#email#','[EMAIL PROTECTED]') works perfect! def insertdata(name, data): file = open('template.html', 'r+') contents = file.read() pos = contents.index(name) file.seek(pos) file.write(data) file.write(contents[pos + len(name):]) file.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
Byron wrote: Bob Gailer wrote: read the file into a string variable (assuming the file is not humungus) find the location of "something" in the string assemble a new string consisting of: the original string up to the location (index) of "something" "what" the rest of the original string write the new string to the file Hi Scott, Bob gave you the basic instructions that you need to complete the task that you are asking for. If you don't know how to do this, I would suggest that you start with the following Python tutorial: http://www.greenteapress.com They provide an excellent tutorial for learning the basics of Python -- and best of all, it's free and written for absolute beginners. Take care, Byron --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thanks again, I figured it out with the index function of the file objects, I didn't know that you could search for a word and find the exact pos of it, that was the tiny bit of information I was looking for. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
Bob Gailer wrote: > read the file into a string variable (assuming the file is not humungus) > find the location of "something" in the string > assemble a new string consisting of: >the original string up to the location (index) of "something" >"what" >the rest of the original string > write the new string to the file Hi Scott, Bob gave you the basic instructions that you need to complete the task that you are asking for. If you don't know how to do this, I would suggest that you start with the following Python tutorial: http://www.greenteapress.com They provide an excellent tutorial for learning the basics of Python -- and best of all, it's free and written for absolute beginners. Take care, Byron --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
At 02:55 PM 8/24/2005, Scott Oertel wrote: >How do I use the built in file objects to insert text into a file at a >certain location? > >i.e. > >something, 2, chance, weee >nothing, happened, crap, nice > > need to search for "something" and insert, "what," before it Here's the algorithm. If you know enough Python you will be able to code it. So put together a program, give it a try and come back with questions. read the file into a string variable (assuming the file is not humungus) find the location of "something" in the string assemble a new string consisting of: the original string up to the location (index) of "something" "what" the rest of the original string write the new string to the file Bob Gailer 303 442 2625 home 720 938 2625 cell ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Working with files
How do I use the built in file objects to insert text into a file at a certain location? i.e. something, 2, chance, weee nothing, happened, crap, nice need to search for "something" and insert, "what," before it thanks for the feedback you guys are great :) -Scott Oertel -Py2.4 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor