Reputation: 321
Is it possible for the user to input a specific directory on their computer, and for Python to write the all of the file/folder names to a text document? And if so, how? The reason I ask is because, if you haven't seen my previous posts, I'm learning Python, and I want to write a little program that tracks how many files are added to a specific folder on a daily basis. This program isn't like a project of mine or anything, but I'm writing it to help me further learn Python. The more I write, the more I seem to learn. I don't need any other help than the original question so far, but help will be appreciated!
Thank you!
EDIT: If there's a way to do this with "pickle", that'd be great! Thanks again!
Upvotes: 2
Views: 8113
Reputation: 548
While the approach of @Makato is certainly right, for your 'diff' like application you want to capture the inode 'stat()' information of the files in your directory and pickle that python object from day-to-day looking for updates; this is one way to do it - overkill maybe - but more suitable than save/parse-load from text-files IMO.
Upvotes: 1
Reputation: 106430
os.walk()
is what you'll be looking for. Keep in mind that this doesn't change the current directory your script is executing from; it merely iterates over all possible paths that stem from the arguments to it.
for dirpath, dirnames, files in os.walk(os.path.abspath(os.curdir)):
print files
# insert long (or short) list of files here
Upvotes: 4
Reputation: 55197
You probably want to be using os.walk
, this will help you in creating a listing of the files down from your directory.
Once you have the listing, you can indeed store it in a file using pickle
, but that's another problem (i.e.: Pickle is not about generating the listing but about storing it).
Upvotes: 2