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.  
  8. def launch():
  9. print "Current working Directory: ", os.getcwd()
  10. print "Contents: ", os.listdir(os.getcwd())
  11. #chdir('foldername') change into folder "foldername"
  12. #mkdir('name') create folder "name"
  13. #mkdirs('name/subname/subsubname')
  14. #rmdir('name') removes if empty
  15. print "Operating System: ", os.name
  16. print "Current directory: ", os.curdir
  17. print "Parent directory: ", os.pardir
  18. #path.isdir('path') true if path is directory
  19. #path.isfile('filename') true if filename is file
  20. #path.exists('path') true if exists
  21. filehandle = file('test.dat', 'w+')
  22. #modi: r,w,a,r+,w+,a+,
  23. #modi: rb,wb,ab (binary) rU, aU (Universal Newline n)
  24. #filehandle.read() read everything from filehandle
  25. #filehandle.readline() read one line
  26. #filehandle.readlines() read everything linewisely
  27. #filehandle.close() close file handle
  28. #filehandle.write() write string to file handle
  29. #filehandle.writelines()
  30. filehandle.write("test")
  31. filehandle.close()
  32. #remove('filename') removes file
  33. #rename('filename_orig', 'filename_new')
  34. #filehandle.mode gets mod (rwx)
  35. #filehandle.closed true if closed
  36. #Utility-Funktionen
  37. #seek(int) set filepointer position
  38. #tell() get filepointer position
  39. #Aufruf
  40. launch()

Report this snippet  

You need to login to post a comment.