Assuming you've got Twisted installed, let's fire up a python interpreter and import the FilePath class and instantiate it with /tmp and test some basic operations:
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
>>> from twisted.python.filepath import FilePath | |
>>> tmp = FilePath('/tmp') | |
>>> tmp.exists() | |
True | |
>>> bogus = tmp.child('bogus') | |
>>> bogus.exists() | |
False |
Isn't that nice? I love not having to import and use the os.path.exists function; it's a method on the object representing a file or a path, as it should be. I also enjoy the convenience FilePath offers when it comes to creating paths:
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
>>> mydir = tmp.child('notbogus').child('anotherdir').child('mydir') | |
>>> mydir.exists() | |
False | |
>>> mydir.makedirs() | |
>>> mydir.restat() | |
>>> mydir.exists() | |
True |
We had to call restat() so that the object would check the file system again (since we just made some changes). Now, for some files:
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
>>> for filename in ['test1.txt', 'test2.txt', 'test3.tst']: | |
... mydir.child(filename).touch() | |
... | |
>>> mydir.listdir() | |
['test1.txt', 'test2.txt', 'test3.tst'] |
And if you don't believe that, we can switch to shell:
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
lorien:oubiwann 20:51:58 $ ls -al /tmp/notbogus/anotherdir/mydir/ | |
total 0 | |
drwxr-xr-x 2 oubiwann wheel 170 Feb 17 20:51 . | |
drwxr-xr-x 3 oubiwann wheel 102 Feb 17 20:46 .. | |
-rw-r--r-- 1 oubiwann wheel 0 Feb 17 20:51 test1.txt | |
-rw-r--r-- 1 oubiwann wheel 0 Feb 17 20:51 test2.txt | |
-rw-r--r-- 1 oubiwann wheel 0 Feb 17 20:51 test3.tst |
Reading and writing operations are the same as usual:
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
>>> myfile = mydir.child(filename) | |
>>> myfile.isdir() | |
False | |
>>> myfile.islink() | |
False | |
>>> myfile.isfile() | |
True | |
>>> fh = myfile.open('w+') | |
>>> fh.write('do the usual thing') | |
>>> fh.close() | |
>>> myfile.getsize() | |
18L | |
>>> myfile.open().read() | |
'do the usual thing' |
But you can use shortcuts like this, too:
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
>>> myfile.getContent() | |
'do the usual thing' | |
>>> myfile.setContent('and now for something completely different...') | |
>>> myfile.getContent() | |
'and now for something completely different...' |
Let's get the path and recheck the file size, just to make sure:
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
>>> myfile.path | |
'/tmp/notbogus/anotherdir/mydir/test3.tst' | |
>>> myfile.restat() | |
>>> myfile.getsize() | |
45L |
Or, again from shell:
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
lorien:oubiwann 20:52:06 $ ls -al /tmp/notbogus/anotherdir/mydir/ | |
total 8 | |
drwxr-xr-x 2 oubiwann wheel 170 Feb 17 20:58 . | |
drwxr-xr-x 3 oubiwann wheel 102 Feb 17 20:46 .. | |
-rw-r--r-- 1 oubiwann wheel 0 Feb 17 20:51 test1.txt | |
-rw-r--r-- 1 oubiwann wheel 0 Feb 17 20:51 test2.txt | |
-rw-r--r-- 1 oubiwann wheel 45 Feb 17 20:58 test3.tst |
Hmm... for some reason, I really like this file and want to keep it. What do I do?
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
>>> docs = FilePath('/Users/oubiwann/Documents') | |
>>> myfile.moveTo(docs.child('special_file.txt')) |
That's it! Nice, eh? Of course, we're careful, so we check to make sure it happened:
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
>>> myfile.exists() | |
False | |
>>> newfile = docs.child('special_file.txt') | |
>>> newfile.exists() | |
True | |
>>> newfile.path | |
'/Users/oubiwann/Documents/special_file.txt' | |
>>> newfile.getsize() | |
45L | |
>>> newfile.getContent() | |
'and now for somethibg completely different...' |
To see more, check out the source, or at least do dir(FilePath) to see what other goodies this class has to offer, and enjoy :-)
No comments:
Post a Comment