Jump to content

Can python redirect a listing of a directory into a file either txt or csv?


G+_Rud Dog
 Share

Recommended Posts

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

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

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

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

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

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

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

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

 Share

×
×
  • Create New...