Created
October 4, 2011 19:59
-
-
Save honzajavorek/1262627 to your computer and use it in GitHub Desktop.
Demo: work with filesystem in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# First line is for OS to know which interpreter to use when executing this file. You can easily determine where is your Python installation | |
# by typing `$ which python` into your bash terminal. You can of course run Python scripts this way: `$ python myscript.py`. | |
# Second line tells interpreter (and most editors, e.g. vim) that your file is in UTF-8. Any non-ASCII characters here, such as áéíóů | |
# will be treated as UTF-8 by Python interpreter thanks to this. See PEP 0263 for further info: http://www.python.org/dev/peps/pep-0263/ | |
import os | |
import getpass | |
import glob | |
import re | |
# Let's try some IO operations. Further reading is here: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files | |
try: | |
with open('/tmp/gargamel', 'w') as f: | |
f.write('I hate smurfs!') | |
except IOError: | |
print 'Gargamel says: Ouch!' | |
# What about reading? | |
try: | |
with open('/tmp/gargamel', 'r') as f: | |
print 'Gargamel says: %s' % f.read() | |
except IOError: | |
print 'Gargamel says: Ouch!' | |
# Okay. I'll omit the try/except blocks for now, you probably got the point of catching exceptions. | |
# Let's play a bit with http://docs.python.org/library/os.html. | |
print 'There are %d items in your temp directory.' % len(os.listdir('/tmp')) | |
# Tip: One great source of wisdom is here http://diveintopython.org/file_handling/os_module.html. | |
user = getpass.getuser() | |
print 'Your home directory contains: %s' % ', '.join(filter(lambda x: not x.startswith('.'), os.listdir('/home/%s/' % user))) | |
# Well, that was a bit more "declarative". We determined current OS user's name, then listed his/her own home directory. | |
# Because we don't want any junk in our listing, we filtered out files which start with dot. Then we got a list of directory items. | |
# We glued it together with ', ' strings and printed out. | |
# Now let's start with something useful. First make some place for it: | |
print '\n\n' | |
# The goal: find mkv files. If there is mp4 file in the same directory and with the same name, set "date modified" to mkv to the same | |
# value as mp4 has. Mind diacritics, Windows box specifics, special characters. | |
def find(dir, condition): | |
"""Recursivly walks through a directory. Generates filenames which satisfy condition.""" | |
dir = os.path.abspath(dir) | |
for f in os.listdir(unicode(dir)): | |
f_path = os.path.join(dir, f) | |
if os.path.isdir(f_path): | |
for f_sub in find(f_path, condition): | |
yield f_sub | |
elif condition(f_path): | |
yield f_path | |
def mkv_to_mp4(mkv_file): | |
"""Returns name of twin mp4 file to given mkv file.""" | |
return re.sub(r'\.mkv$', '.mp4', mkv_file) | |
def cond(file): | |
"""Condition for searching: only mkv files, which have twin mp4 file.""" | |
return re.search(r'\.mkv$', file) and os.path.isfile(mkv_to_mp4(file)) | |
# Now the final (not properly tested!) piece of code. Using docs.python.org/library/os.html | |
for mkv_file in find('/tmp/python-script-test/', cond): # on Windows box: 'D:\\tmp\\python-script-test\\' | |
mp4_file = mkv_to_mp4(mkv_file) # get name of twin mp4 file | |
stat = os.stat(mp4_file) # get stat of twin mp4 file | |
os.utime(mkv_file, (stat.st_atime, stat.st_mtime)) # save times to mkv files | |
# Maybe there is even easier way :) . And trust me, there usually is something easier... | |
# and also more "pythonish", more declarative, more dependent on Python protocols... :) The language always suprises me. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment