Python File System Operation Snippets Collection
In this post, I'll share some simple snippet to do various file system operation. Pretty much as a bookmark for my own usage, in case I needed it someday.
How to get current working directory:
import os os.getcwd()
How to change working directory:
import os
os.chdir('/tmp/')
How to list directory contents:
import os
os.listdir('/tmp/')
How to check if directory / file exists:
import os
os.path.exists('/tmp/')#work for both file and directory
How to create directory:
import os
dir = os.path.dirname('/tmp/current_object/')
if not os.path.exists(d):
os.makedirs(d)
Using code above will ensure you did not create directory that already exists
How to delete file or directory:
import os
os.remove('/tmp/')
or if you want to delete non empty directory:
import shutil
shutil.rmtree('/tmp/somefolder')
How to create temporary directory:
import os
import tempfile
import shutil
#let OS decide where they want to create the temp directory
tmpdir = tempfile.mkdtemp('somesuffix', 'someprefix')
#You want to take control where you want the temp directory be created
tmpdir = tempfile.mkdtemp('somesuffix', 'someprefix', '/tmp/path/of/your/choice/')
#be sure to remove the temp directory once work is done
shutil.rmtree('/tmp/somefolder')
How to create temporary file:
import os
import tempfile
#let OS decide where they want to create the temp directory
tmpfile = tempfile.mkstemp('somesuffix', 'someprefix', None, 'some initial contents')
#You want to take control where you want the temp directory be created
tmpfile = tempfile.mkstemp('somesuffix', 'someprefix', '/tmp/path/of/your/choice/', 'some initial contents')
#be sure to remove the temp directory once work is done
os.remove(tmpfile)
How to check file permission:
import os import stat file = '/tmp/' stt = os.stat(file) mode = stt[stat.ST_MODE] print "%s mode is: %s" % (file, mode)
Employee Wasting Company Time? Spy on Them
An employee looks very serious in front of the computer. His hands move very fast in his keyboards. It's like he's working very hard. But after that, suddenly he is smiling and even laughing something even tough nothing is funny around him. What does he do exactly?
Automate Facebook Status Update Without Facebook API
Sweet little snippet with only 10 lines of code! Using python mechanize library to update your facebook status. Check out the code:
Multiple Boot Partition Tips
Yesterday, I had a chance to install ubuntu 9.10 'karmic koala' on my laptop. Having read several online reviews made me rethink my strategy. Previously, whenever there's new ubuntu release, I will always do fresh install. Now, several minor but annoying bug appeared in reviews where I had similar hardware
. Kind of annoying, but, I'll still try it.
Using Dropbox For Easy File Sharing
I share docs with my colleague every now and then. Previously, I would have sent it as email attachment or upload it somewhere. But, from the moment I use dropbox, sharing documents has never been this easy!









