Posted By

dalcacer on 03/09/10


Tagged

file


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


Filehandling


 / Published in: Python
 

Shows basic filehandling with python.

  1. #!/usr/bin/env python
  2. import sys,os
  3. from time import *
  4. # This is my second pythonscript.
  5. # Its intention is to get used to fileaccess.
  6. # This program doesn't serve a certain purpose.
  7. # @author: [email protected]
  8.  
  9. def launch():
  10. print "Current working Directory: ", os.getcwd()
  11. print "Contents: ", os.listdir(os.getcwd())
  12. #chdir('foldername') change into folder "foldername"
  13. #mkdir('name') create folder "name"
  14. #mkdirs('name/subname/subsubname')
  15. #rmdir('name') removes if empty
  16. print "Operating System: ", os.name
  17. print "Current directory: ", os.curdir
  18. print "Parent directory: ", os.pardir
  19. #path.isdir('path') true if path is directory
  20. #path.isfile('filename') true if filename is file
  21. #path.exists('path') true if exists
  22. filehandle = file('test.dat', 'w+')
  23. #modi: r,w,a,r+,w+,a+,
  24. #modi: rb,wb,ab (binary) rU, aU (Universal Newline n)
  25. #filehandle.read() read everything from filehandle
  26. #filehandle.readline() read one line
  27. #filehandle.readlines() read everything linewisely
  28. #filehandle.close() close file handle
  29. #filehandle.write() write string to file handle
  30. #filehandle.writelines()
  31. filehandle.write("test")
  32. filehandle.close()
  33. #remove('filename') removes file
  34. #rename('filename_orig', 'filename_new')
  35. #filehandle.mode gets mod (rwx)
  36. #filehandle.closed true if closed
  37. #Utility-Funktionen
  38. #seek(int) set filepointer position
  39. #tell() get filepointer position
  40. #Aufruf
  41. launch()

Report this snippet  

You need to login to post a comment.