G+_Rud Dog Posted June 21, 2015 Share Posted June 21, 2015 Can python redirect a listing of a directory into a file either txt or csv? Looking for something similar to unix or DOS where you can just type something like: dir > listing.txt Link to comment Share on other sites More sharing options...
G+_Jeff Brand Posted June 21, 2015 Share Posted June 21, 2015 Yes. Disclaimer: I don't know Python. :) You just need to write a script that sends console output (stdout) in the format you want. The end result would be just as simple: dir.py > listing.csv Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted June 24, 2015 Share Posted June 24, 2015 If you mean redirecting where Python sends print(), you just do it the same way the shell does, by duping file descriptors. Here's an example: http://stackoverflow.com/questions/8817993/dup-dup2-tmpfile-and-stdout-in-python Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 24, 2015 Author Share Posted June 24, 2015 Maybe I should have placed a disclaimer on my entry as well. I copied the code read through it then attempted to run it getting: OSError was unhandled by user code Message: [WinError 126] The specified module could not be found Since I don't understand the code will have to keep searching. Just wish unix environment came with all computers as standard could us my knowledge from writing scripts and be done with it. Oh well the search goes on. Link to comment Share on other sites More sharing options...
G+_Jeff Brand Posted June 24, 2015 Share Posted June 24, 2015 Lee Crocker , very interesting article though I think it qualified under "special I/O tricks not for beginners." :) Rudy Trujillo , it looks like an exception of the OSError type is being thrown because a module couldn't load. This could be a path spec error or missing file. Got a pastebin, gist, or other code snippet we could look at? As for the Unix environment, you could try 1) Cygwin, 2) use a VM (my preference), 3) Install *NIX, Ubuntu Linux is a common choice. 4) Get a Mac. The idiosyncrasies of Windows' half-implemented stdout/pipes are frustrating but will get the task mentioned in the OP done. Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 25, 2015 Author Share Posted June 25, 2015 Thanks Jeff and Lee, always looking for a way to run Unix on a Windows machine but my problem is can't share it with friends and family unless the software running it is also installed. Guess I was just venting but thanks again. Oh and we are a split OS family Macs and PCs but my area of interest at the moment is the PC environment. Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 25, 2015 Author Share Posted June 25, 2015 Funny ran across this when reviewing a financial python script: This post will be short but very informative. You can learn a few good Unix/Linux tricks on the way. The goal is well defined in the title. So, what’s the quickest solution? We will make use of Python in the Unix-based environment. As you will see, for any text file, writing a single line of Unix commands is more than enough to deliver exactly what we need (a basic text file processing). If you try to do the same in Windows.. well, good luck! Link to comment Share on other sites More sharing options...
G+_Jeff Brand Posted June 25, 2015 Share Posted June 25, 2015 Here you go: https://gist.github.com/deltafactory/6e6e93d7e3c3d23698cd os.listdir() needed to be used because the more-powerful os.walk() is Unix only. Link to comment Share on other sites More sharing options...
G+_Jeff Brand Posted June 25, 2015 Share Posted June 25, 2015 Also, consider this for final "distribution" without needing a full install. http://www.py2exe.org/ Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 26, 2015 Author Share Posted June 26, 2015 Thanks Jeff, bookmarked can't wait to review but got some things need doing again thanks. Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 26, 2015 Author Share Posted June 26, 2015 Liking the idea of creating executable from a python script. Unfortunately could not get the code below to run getting "IndexError: List index out of range. Referring to line assigning path a variable : After looking at the second line of code it appears we are trying to assign a windows path to "path" not really sure why we would be calling array second storage location and why we would be running length on the same argv? It appears if this fails we go with the current working directory. The key here is no idea what sys.argv is doing:) import os, sys path = sys.argv[1] if len( sys.argv ) else '.' # CSV Header print( 'Filename', 'Type', 'Size', 'Modified Time', sep="\t" ) for f in os.listdir(path): filepath = os.path.join( path, f ) fod = 'dir' if os.path.isdir( filepath ) else 'file' try: # This throws an error when there's insufficient permissions. fs = os.stat(filepath) fsinfo = [fs.st_size, fs.st_mtime] except: fsinfo = ['', ''] print( f, fod, fsinfo[0], fsinfo[1], sep="\t") Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 26, 2015 Author Share Posted June 26, 2015 Traceback (most recent call last): File "c:dir.py", line 3, in path = sys.argv[1] if len( sys.argv ) else '.' IndexError: list index out of range Press any key to continue . . . Link to comment Share on other sites More sharing options...
G+_Jeff Brand Posted June 26, 2015 Share Posted June 26, 2015 That line uses Python 3's ternary operator. Are you using Python 2? I also didn't do an explicit check for > 1 and don't know enough about python's truthiness conversions to know if it would work as expected. I thought I tested it before publishing.. This should work in place of that line: if len( sys.argv ) > 1: path = sys.argv[1] else: path = '.' Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 26, 2015 Author Share Posted June 26, 2015 Yeah running Python 3, sorry should have mentioned that. This is the kind of coding I am excited about and would love to have code as simple as the one shown that is failing explained in english. Only problem I have, keep finding great delivery on videos but same old basics covered. Nothing against basics lawd knows I need them but breaking down the code used here would help my understanding of Python. As for you testing it don't worry 99% of the time is is something dumb on my part. Link to comment Share on other sites More sharing options...
G+_Jeff Brand Posted June 26, 2015 Share Posted June 26, 2015 Python 3 is no problem (probably better!) and the ternary worked as it should. The error was entirely my fault. Docs coming.. Link to comment Share on other sites More sharing options...
G+_Jeff Brand Posted June 26, 2015 Share Posted June 26, 2015 The gist is now plentifully commented. Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 26, 2015 Author Share Posted June 26, 2015 Thank you Jeff, on my way. Link to comment Share on other sites More sharing options...
G+_Rud Dog Posted June 26, 2015 Author Share Posted June 26, 2015 Thought I would echo here: Wanted to take a minute to thank you for the detailed comments you added to your code it was clear and concise. You went beyond what most would have done. Again thank you for your help understanding some of the rudimentary aspects of coding in python. Link to comment Share on other sites More sharing options...
G+_Jeff Brand Posted June 26, 2015 Share Posted June 26, 2015 It's my pleasure. I write code for a living, largely fueled by an interest in problem solving and understanding how things work. This was a great first exercise by which to learn Python and I'm glad I could share the results. Link to comment Share on other sites More sharing options...
Recommended Posts