Re: [Tutor] WRITING XLS FROM OS.WALK()
Hello Walter and members: I'm working with csv to handle excel, my other was useful to do my new cvs file. When I open the output file, it has all the information I need, but all of my lines have the filepath separate by comes, even though it's supposed to be a complete string; for example: C,:,\,i,n,d,i,c,e,.,s,h,p all the others have the same format. Besides I need a row to put in each cell the column names, with: c.writerow(["Archivo","x_min","x_max","y_min","y_max","geometria","num_ele","prj","estructura bd","extent","fecha_modificacion","maquina_host"]). But all tha names gather in the same row and I want my pathfile to place under the "Archivo" column, but I couldn't do it. 2010/10/11 Walter Prins > > > On 11 October 2010 14:37, Susana Iraiis Delgado Rodriguez < > susana.delgad...@utzmg.edu.mx> wrote: > >> The other question is about the excel files management, if I want to >> manipulate this data to work it in Excel should I look for an excel library >> in python right? >> >> > Yes. I already mentioned one (in my view good) option in my original reply > to you. The xlwt module works quite well for generating Excel files (with > expectable limitations) from any platform that Python's available on (e.g. > including non-Windows.) and thus does not require Excel to be available on > the machine you're producing the file on. > > If however you are running on Windows and have Excel installed, you could > consider driving the real Excel via COM automation, which will guarantee you > get desired results including formatting, charts etc, and will ensure you > have full access to all the functionality Excel exposes via its COM object > model. > > If your requirements on the other hand simple enough then Joel's suggestion > to use CSV is probably preferable. (The KISS principle: "Keep It Small & > Simple") > > There's also the possibility to look into generating .xlsx files (e.g. XML > based Office format files.) Theoretically you should be able to generate > the correctly structured XML independent of Excel and have it open in > Excel. I suspect that may be rather painful though (have never tried this > myself) andam almost reluctant to even mention this option, so caveat > emptor. > > Walter > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On 11 October 2010 14:37, Susana Iraiis Delgado Rodriguez < susana.delgad...@utzmg.edu.mx> wrote: > The other question is about the excel files management, if I want to > manipulate this data to work it in Excel should I look for an excel library > in python right? > > Yes. I already mentioned one (in my view good) option in my original reply to you. The xlwt module works quite well for generating Excel files (with expectable limitations) from any platform that Python's available on (e.g. including non-Windows.) and thus does not require Excel to be available on the machine you're producing the file on. If however you are running on Windows and have Excel installed, you could consider driving the real Excel via COM automation, which will guarantee you get desired results including formatting, charts etc, and will ensure you have full access to all the functionality Excel exposes via its COM object model. If your requirements on the other hand simple enough then Joel's suggestion to use CSV is probably preferable. (The KISS principle: "Keep It Small & Simple") There's also the possibility to look into generating .xlsx files (e.g. XML based Office format files.) Theoretically you should be able to generate the correctly structured XML independent of Excel and have it open in Excel. I suspect that may be rather painful though (have never tried this myself) andam almost reluctant to even mention this option, so caveat emptor. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Mon, Oct 11, 2010 at 9:37 AM, Susana Iraiis Delgado Rodriguez < susana.delgad...@utzmg.edu.mx> wrote: > Hello members! > > Thank you all of you for taking the time to answer to my question. I didn´t > expect it will create the controversy we just had in this topic. I don't > mind for the comments of re-inventing the wheel, I'm new in python, > documentation is good but programmers' experience is even better. > > The intention of using the os.walk() is because I'm creating a sort of > directory crawler to find every file with the extensions I'm looking for, so > am I in the rigth direction? > The other question is about the excel files management, if I want to > manipulate this data to work it in Excel should I look for an excel library > in python right? > > > > I would write the file as csv. Then, excel, or any other spreadsheet can read the file. Python offers csv support, its easy to understand, produces small files, and being a text format, it is easy to debug problems. -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
Hello members! Thank you all of you for taking the time to answer to my question. I didn´t expect it will create the controversy we just had in this topic. I don't mind for the comments of re-inventing the wheel, I'm new in python, documentation is good but programmers' experience is even better. The intention of using the os.walk() is because I'm creating a sort of directory crawler to find every file with the extensions I'm looking for, so am I in the rigth direction? The other question is about the excel files management, if I want to manipulate this data to work it in Excel should I look for an excel library in python right? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
"Steven D'Aprano" wrote Only if glob now descends into the file system... which is why you'd choose os.walk instead. Do you mean you want to point at a single directory and have it search any and all subdirectories, no matter how deeply nested? Well, yes, that would be a good use-case for os.walk, and yes, I completely missed it, so I withdraw my comment that Susana had re-invented the wheel. I think the solution should use both. os.walk to traverse the directories and glob to build the file list at each level. Each tool has its role. I didn't see anything untoward in recommending glob to replace the use of the string based list comp for selection of the files, I just assemed you meant in conjunction with os.walk!. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Sun, 10 Oct 2010 03:59:26 am Emile van Sebille wrote: > On 10/8/2010 8:55 PM Steven D'Aprano said... > > > I'm sorry to tell you that you've just reinvented the wheel. This > > was already solved, a long, long time ago. It is called the glob > > module: > > Only if glob now descends into the file system... which is why you'd > choose os.walk instead. Do you mean you want to point at a single directory and have it search any and all subdirectories, no matter how deeply nested? Well, yes, that would be a good use-case for os.walk, and yes, I completely missed it, so I withdraw my comment that Susana had re-invented the wheel. Mea culpa, sorry Susana for any confusion I caused. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
"David Hutto" wrote I'll put my questions in a knew thread if this is considered hijacking the OP. Well, it doesn''t have much to do with XLS files thats for sure so yes, if you are interested in further pursuing approaches to teaching programming a separate thread is probably better. :-) Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
I'll put my questions in a knew thread if this is considered hijacking the OP. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Sat, Oct 9, 2010 at 2:23 PM, David Hutto wrote: > On Sat, Oct 9, 2010 at 1:58 PM, Alan Gauld wrote: >> >> "David Hutto" wrote should learn to program in assembler (or even microcode) then move to C and then to Python(or similar) and finally to 4G languages. >>> >>> Even old schooler though would be that we're just directing electrical >>> flow from an ac outlet through a dc converter and streaming it through >>> the circuit board though. Which is not as easy as it sounds! >> >> Yeah, but I don't think anyone would claim that was programming! :-) >> >> Although I did first learn my boolean logic by having to translate >> it into switch circuits - real mechanical toggle switches not >> electonics... >> AND => two switches in series >> OR = two switches in parallel >> NOT = an "off" switch - ie. on by default >> etc/... >> >> We then moved to relays and finally diodes and resistors... >> >> But that was training us in electronics not programming. >> > > How can you distinguish the two, once learned though? I don't know C > but I do know python, and the what's below the BIOS(muy basically). Not How, but maybe , more importantly, why? To avoid the low-level programming confusion, or for more control over user gui's(Meaning client wants)? > > >> Alan G. >> >> >> ___ >> Tutor maillist - tu...@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Sat, Oct 9, 2010 at 1:58 PM, Alan Gauld wrote: > > "David Hutto" wrote >>> >>> should learn to program in assembler (or even microcode) then move >>> to C and then to Python(or similar) and finally to 4G languages. >> >> Even old schooler though would be that we're just directing electrical >> flow from an ac outlet through a dc converter and streaming it through >> the circuit board though. Which is not as easy as it sounds! > > Yeah, but I don't think anyone would claim that was programming! :-) > > Although I did first learn my boolean logic by having to translate > it into switch circuits - real mechanical toggle switches not > electonics... > AND => two switches in series > OR = two switches in parallel > NOT = an "off" switch - ie. on by default > etc/... > > We then moved to relays and finally diodes and resistors... > > But that was training us in electronics not programming. > How can you distinguish the two, once learned though? I don't know C but I do know python, and the what's below the BIOS(muy basically). > Alan G. > > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
"David Hutto" wrote should learn to program in assembler (or even microcode) then move to C and then to Python(or similar) and finally to 4G languages. Even old schooler though would be that we're just directing electrical flow from an ac outlet through a dc converter and streaming it through the circuit board though. Which is not as easy as it sounds! Yeah, but I don't think anyone would claim that was programming! :-) Although I did first learn my boolean logic by having to translate it into switch circuits - real mechanical toggle switches not electonics... AND => two switches in series OR = two switches in parallel NOT = an "off" switch - ie. on by default etc/... We then moved to relays and finally diodes and resistors... But that was training us in electronics not programming. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
"David Hutto" wrote And in other news, is it better to be a programmer, or an electrician first? Well, I started as an electronic engineer writing software, then became a software engineer writing software for electronic engineers then gradually the software got bigger and more abstract and now I virtually never even think about the electronics In fact nowadays I spend most of my time discussing high level design where I'm not even thinking too much about the actual code, the focus is all on module structures, frameworks and library definitions etc. Playing with Python helps keep me grounded :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On 10/8/2010 8:55 PM Steven D'Aprano said... I'm sorry to tell you that you've just reinvented the wheel. This was already solved, a long, long time ago. It is called the glob module: Only if glob now descends into the file system... which is why you'd choose os.walk instead. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
And in other news, is it better to be a programmer, or an electrician first? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Sat, Oct 9, 2010 at 4:12 AM, Alan Gauld wrote: > "David Hutto" wrote > >> > I'm sorry to tell you that you've just reinvented the wheel. This > was >> > already solved, a long, long time ago. It is called the glob > module: >> >> Hey, buddy pal. Isn't it true that newbs should take of advantage of >> the fact that you have to solve the problem pythonically > > Thats a matter of great dispute. There is an old school that says you > should learn to program in assembler (or even microcode) then move > to C and then to Python(or similar) and finally to 4G languages. Even old schooler though would be that we're just directing electrical flow from an ac outlet through a dc converter and streaming it through the circuit board though. Which is not as easy as it sounds! > Then there is a new school that says life is too short, learn to > program like a professional - use the highest level language you > can and leverage the libraries. > > Personally I would say for the hobbyist, new-school is best. The real > skill to learn is finding the best library and figuring out how to use it. But I think higher level should be for productivity, but low level for higher knowledge of the productivity. > [For a pro doing a 4 year computing course then there is still a lot > to be said in starting from scratch and building up - they may > have to do it that way someday on new hardware or creating a new OS, > but an amateur is never likely to be building from scratch...] > Unless you're a technological masochist. >> > You should use that. It works, it is tested and thoroughly > debugged, >> > and >> > it is powerful. >> >> Certainly so, but not as powerful as the individual's ingenuity in >> solving the problem at hand without foreknowledge of the 'known' >> solution. > > That depends on how good the student is. It is very unlikely > that the student will come up with anything close to globs > power and flexibility on their first attempt. So if they want a learning > exercise it might be an interesting task, but if they want to actually > achieve a result they should use glob. > > Software reuse is a big issue in the industry just now and a lot > of effort is being spent in getting software engineers out of the > "not invented here" mentality and into the reuse mentality. So > encouraging beginners to get into the habit of "scavenge and > adapt" is actually in line with current industry thinking. Then I'm apparently an industry thinker. > > Just as an alternative view... :-) > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Fri, Oct 8, 2010 at 3:34 PM, Susana Iraiis Delgado Rodriguez wrote: > for i in allfiles: > print i > a.write(i) > a.write("\n") Are you sure you want to write bare data to a file ending in .xls? That might be confusing Excel, unless it has grown a sizable brain with respect to file handling. You might investigate the csv module that Python offers [1], and use it to write CSV. Excel can import that very easily. Hell, simply writing a list of filenames delimited by \n might be importable into Excel. [1]: http://docs.python.org/library/csv.html -- Jed Smith j...@jedsmith.org ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
"Steven D'Aprano" wrote The specialist replied "The bill is $10 for my time, and $490 for knowing which was the right tool to use." Interesting modern variant on the version I heard A man's washing machine broke down so he called the plumber. The plumber looks at it for a few minutes then takes a hammer out of his toolkit. He gently taps a fixing and the machine starts working. He presents a bill for $50. The man is shocked, you only tapped it with a hammer how can you justify that? The plumber amends the bill: $1 for hitting with hammer, $49 for knowing where to hit, and how hard. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
"David Hutto" wrote > I'm sorry to tell you that you've just reinvented the wheel. This > was > already solved, a long, long time ago. It is called the glob > module: Hey, buddy pal. Isn't it true that newbs should take of advantage of the fact that you have to solve the problem pythonically Thats a matter of great dispute. There is an old school that says you should learn to program in assembler (or even microcode) then move to C and then to Python(or similar) and finally to 4G languages. Then there is a new school that says life is too short, learn to program like a professional - use the highest level language you can and leverage the libraries. Personally I would say for the hobbyist, new-school is best. The real skill to learn is finding the best library and figuring out how to use it. [For a pro doing a 4 year computing course then there is still a lot to be said in starting from scratch and building up - they may have to do it that way someday on new hardware or creating a new OS, but an amateur is never likely to be building from scratch...] > You should use that. It works, it is tested and thoroughly > debugged, and > it is powerful. Certainly so, but not as powerful as the individual's ingenuity in solving the problem at hand without foreknowledge of the 'known' solution. That depends on how good the student is. It is very unlikely that the student will come up with anything close to globs power and flexibility on their first attempt. So if they want a learning exercise it might be an interesting task, but if they want to actually achieve a result they should use glob. Software reuse is a big issue in the industry just now and a lot of effort is being spent in getting software engineers out of the "not invented here" mentality and into the reuse mentality. So encouraging beginners to get into the habit of "scavenge and adapt" is actually in line with current industry thinking. Just as an alternative view... :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
Here's a little 'anomaly' though, programmers say hammer when the truth is, that modern construction should use pneumatic tools, as in air hammer. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Sat, Oct 9, 2010 at 2:12 AM, Steven D'Aprano wrote: > On Sat, 9 Oct 2010 03:15:35 pm you wrote: > >> > You should use that. It works, it is tested and thoroughly >> > debugged, and it is powerful. >> >> Certainly so, but not as powerful as the individual's ingenuity in >> solving the problem at hand without foreknowledge of the 'known' >> solution. > > I suppose you made your own computer, smelting your own ores to get the > metals and creating your own plastics from oil you dug up yourself, Sometimes breaking it down to bear essentials is wht you need, and sometimes, you need to take advantage of the 'frosting' a language provides > right? And then you wrote your own operating system, and wrote your own > programming language which just happened to be exactly the same as > Python in every possible way. Not in every way. but a way that makes sense to a demographic, just the way python overwrites(wraps faster languages within it's compiled, then .pyc's it to compiled), several other languages that are the same or even more advocated to the new computer scientist. > > My uncle once went to the doctor complaining about general ill-health. > The doctor sent him to a specialist, who examined him for five minutes, > ran a blood sample through a little hand-held device, and two minutes > later said "You've got such-and-such a disease. Here's my bill for > $500." Specialists always charge extra, and this is new how? > > My uncle got all indignant. "$500? You've hardly done anything! Why > should you get so much just because you've got a tool that does the > work for you?" > > The specialist replied "The bill is $10 for my time, and $490 for > knowing which was the right tool to use." A hammer can do the same in some instances, just the same as a pair of wire cutters tapping the same nail in. Both get the job done, but experience dictates the knowledge one uses for the fastest result, eg. the price one is paid for the time they can do it in->project vs per hour. > > > -- > Steven D'Aprano > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Sat, 9 Oct 2010 03:15:35 pm you wrote: > > You should use that. It works, it is tested and thoroughly > > debugged, and it is powerful. > > Certainly so, but not as powerful as the individual's ingenuity in > solving the problem at hand without foreknowledge of the 'known' > solution. I suppose you made your own computer, smelting your own ores to get the metals and creating your own plastics from oil you dug up yourself, right? And then you wrote your own operating system, and wrote your own programming language which just happened to be exactly the same as Python in every possible way. My uncle once went to the doctor complaining about general ill-health. The doctor sent him to a specialist, who examined him for five minutes, ran a blood sample through a little hand-held device, and two minutes later said "You've got such-and-such a disease. Here's my bill for $500." My uncle got all indignant. "$500? You've hardly done anything! Why should you get so much just because you've got a tool that does the work for you?" The specialist replied "The bill is $10 for my time, and $490 for knowing which was the right tool to use." -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Fri, Oct 8, 2010 at 11:55 PM, Steven D'Aprano wrote: > On Sat, 9 Oct 2010 06:34:44 am Susana Iraiis Delgado Rodriguez wrote: >> Hello members: >> I developed a Python module to make a list which contains all the >> files ending with .shp and .dbf extensions, I have solved this >> already, > > > I'm sorry to tell you that you've just reinvented the wheel. This was > already solved, a long, long time ago. It is called the glob module: Hey, buddy pal. Isn't it true that newbs should take of advantage of the fact that you have to solve the problem pythonically(as in by yourself), even if the function already exists? If I get the gist of thinikig like a programmer. > import glob glob.glob("/home/steve/*.jpg") > ['/home/steve/hoversonic.jpg', '/home/steve/seperated_at_birth.jpg'] glob.glob("/home/steve/*.txt") > ['/home/steve/woss.txt', '/home/steve/file.txt', '/home/steve/post.txt'] > > > You should use that. It works, it is tested and thoroughly debugged, and > it is powerful. Certainly so, but not as powerful as the individual's ingenuity in solving the problem at hand without foreknowledge of the 'known' solution. > > >> but now I want to write an excel file from it. The file >> should show the full path from the found files. > > > Excel files are a proprietary, secret, binary file format. There is a > Python project to allow reading and writing Excel files, but since it > has to reverse-engineer the secret format, there's no guarantee that it > will work. Having said that, I believe that it is very reliable, but > I've never used it myself. > > Google on "python read write excel files" for more information. > > However, if your only aim is to make the data available to Excel, and > you don't care what sort of file you use, the best way is the standard > interchange format between spreadsheet applications, the comma- > separated value file, or CSV. This is a plain-text file, and Python > comes with a module for reading and writing them. From the interactive > interpreter, run this for more information: > > import csv > help(csv) > > > >> This is the code: >> >> import os >> a = open ("directorio.xls","w") > > Just because you name a file .xls doesn't make it an actual Excel file, > any more than taking a JPEG and renaming it "word.exe" would turn it > into the Microsoft Word application. > > >> allfiles = [] #store all files found >> for root,dir,files in os.walk("C:\\"): >> filelist = [ os.path.join(root,fi) for fi in files if >> fi.endswith(".shp") or fi.endswith(".dbf") ] > > > This isn't your actual code. I know this, because the indentation is > broken and it gives a syntax error. > >> for f in filelist: >> allfiles.append(f) > > This is better written as: > > allfiles.extend(filelist) > > > but of course it is better to use the glob module. > >> for i in allfiles: >> print i >> a.write(i) >> a.write("\n") > > The name "i" is normally used for integers, not file names. It would be > better to write that as: > > for filename in allfiles: > print filename > a.write(filename + '\n') > > > > -- > Steven D'Aprano > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > No problems here yet though buddy. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On Sat, 9 Oct 2010 06:34:44 am Susana Iraiis Delgado Rodriguez wrote: > Hello members: > I developed a Python module to make a list which contains all the > files ending with .shp and .dbf extensions, I have solved this > already, I'm sorry to tell you that you've just reinvented the wheel. This was already solved, a long, long time ago. It is called the glob module: >>> import glob >>> glob.glob("/home/steve/*.jpg") ['/home/steve/hoversonic.jpg', '/home/steve/seperated_at_birth.jpg'] >>> glob.glob("/home/steve/*.txt") ['/home/steve/woss.txt', '/home/steve/file.txt', '/home/steve/post.txt'] You should use that. It works, it is tested and thoroughly debugged, and it is powerful. > but now I want to write an excel file from it. The file > should show the full path from the found files. Excel files are a proprietary, secret, binary file format. There is a Python project to allow reading and writing Excel files, but since it has to reverse-engineer the secret format, there's no guarantee that it will work. Having said that, I believe that it is very reliable, but I've never used it myself. Google on "python read write excel files" for more information. However, if your only aim is to make the data available to Excel, and you don't care what sort of file you use, the best way is the standard interchange format between spreadsheet applications, the comma- separated value file, or CSV. This is a plain-text file, and Python comes with a module for reading and writing them. From the interactive interpreter, run this for more information: import csv help(csv) > This is the code: > > import os > a = open ("directorio.xls","w") Just because you name a file .xls doesn't make it an actual Excel file, any more than taking a JPEG and renaming it "word.exe" would turn it into the Microsoft Word application. > allfiles = [] #store all files found > for root,dir,files in os.walk("C:\\"): >filelist = [ os.path.join(root,fi) for fi in files if > fi.endswith(".shp") or fi.endswith(".dbf") ] This isn't your actual code. I know this, because the indentation is broken and it gives a syntax error. >for f in filelist: > allfiles.append(f) This is better written as: allfiles.extend(filelist) but of course it is better to use the glob module. > for i in allfiles: > print i > a.write(i) > a.write("\n") The name "i" is normally used for integers, not file names. It would be better to write that as: for filename in allfiles: print filename a.write(filename + '\n') -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
Hi ! Being Python as rich in libraries, probably there's already a library to create .XLS files. Before finding that, you can try the CSV format: simply put a comma to separate any fields you want in your values. And of course, a comma will finish your line. That way, Excel or any other spreadsheet program will understand what you want them to understand. All the best, hilton On Fri, Oct 8, 2010 at 6:08 PM, Walter Prins wrote: > > > On 8 October 2010 20:34, Susana Iraiis Delgado Rodriguez > wrote: >> >> Hello members: >> I developed a Python module to make a list which contains all the files >> ending with .shp and .dbf extensions, I have solved this already, but now I >> want to write an excel file from it. The file should show the full path from >> the found files. This is the code: >> >> import os >> a = open ("directorio.xls","w") > > > Excel (.xls) is not a text format. What you've written opens > "directorio.xls" as a text file. > > If you want to write an Excel format file, have a look at the xlwt (and the > xlrt for reading) Python modules. See http://www.python-excel.org/ > > Walter > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On 10/8/2010 12:34 PM Susana Iraiis Delgado Rodriguez said... Hello members: I developed a Python module to make a list which contains all the files ending with .shp and .dbf extensions, I have solved this already, but now I want to write an excel file from it. The file should show the full path from the found files. This is the code: import os a = open ("directorio.xls","w") allfiles = [] #store all files found for root,dir,files in os.walk("C:\\"): filelist = [ os.path.join(root,fi) for fi in files if fi.endswith(".shp") or fi.endswith(".dbf") ] for f in filelist: allfiles.append(f) for i in allfiles: print i a.write(i) a.write("\n") With the code above, I have the print and the .xls file with this information in it, the issue here is that my file doesn't have the complete information that I got in the console. Any idea? The last line from excel is C:\Python26 You may find that finishing with a.flush() and a.close() fixes your problem. Also, it appears that you're doing more than is required -- specifically, looping through filelist appending its members to allfiles can be done more simply with append, so where you're saying: filelist = [... you could say allfiles.extend([... and forget about filelist entirely. HTH, Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] WRITING XLS FROM OS.WALK()
On 8 October 2010 20:34, Susana Iraiis Delgado Rodriguez < susana.delgad...@utzmg.edu.mx> wrote: > Hello members: > I developed a Python module to make a list which contains all the files > ending with .shp and .dbf extensions, I have solved this already, but now I > want to write an excel file from it. The file should show the full path from > the found files. This is the code: > > import os > a = open ("directorio.xls","w") > Excel (.xls) is not a text format. What you've written opens "directorio.xls" as a text file. If you want to write an Excel format file, have a look at the xlwt (and the xlrt for reading) Python modules. See http://www.python-excel.org/ Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] WRITING XLS FROM OS.WALK()
Hello members: I developed a Python module to make a list which contains all the files ending with .shp and .dbf extensions, I have solved this already, but now I want to write an excel file from it. The file should show the full path from the found files. This is the code: import os a = open ("directorio.xls","w") allfiles = [] #store all files found for root,dir,files in os.walk("C:\\"): filelist = [ os.path.join(root,fi) for fi in files if fi.endswith(".shp") or fi.endswith(".dbf") ] for f in filelist: allfiles.append(f) for i in allfiles: print i a.write(i) a.write("\n") With the code above, I have the print and the .xls file with this information in it, the issue here is that my file doesn't have the complete information that I got in the console. Any idea? The last line from excel is C:\Python26 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor