Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: Python  |  size: 1.60 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #Enable the os module
  2. import os
  3.  
  4.  
  5. #detect the OS if possible
  6. print "Your running " + os.name
  7. #print the current directory
  8. print "You are currently at " + os.getcwd();
  9.  
  10. print "**********************************************"
  11.  
  12. #here are the files inside your current location
  13. print "Here are your files in your working directory."
  14. for i in os.listdir(os.curdir):
  15.     print i
  16.  
  17. print "**********************************************"
  18.  
  19. #show the file connector for your file system
  20. print "Your file seperator is:" + os.path.join(" "," ")
  21.  
  22. print "**********************************************"
  23. #now for something advanced use the current directory, iterate through and distinguish files from folders
  24.  
  25. for i in os.listdir(os.curdir):
  26.     cur_item = os.path.join(os.getcwd(),i)
  27.     if(os.path.isdir(cur_item)):
  28.            print i + " <- Directory"
  29.     elif (os.path.isfile(cur_item)):
  30.            print i + " <- File"
  31.     else:  #the item may be a link or other special structure
  32.            print i + " is not a file nor directory."
  33.        
  34. # now lets create and delete some data
  35.  
  36. print "Creating a file "
  37. my_file = open("greets","w")
  38. my_file.write("This is the first line of the greets file")
  39. my_file.close()
  40.  
  41. my_file = open("greets","r")
  42. print my_file.readline()
  43. my_file.close()
  44. os.remove("greets")
  45. print "Deleted file"
  46.  
  47. #make a directory os.makedirs will make intermedite directories os. mkdir doesn't
  48. os.mkdir("MyDirectory")
  49.  
  50. #delete it now only works for empty directories
  51. # to tear out a whole directory tree, shutil.rmtree can be used
  52. os.rmdir("MyDirectory")
  53.  
  54.  
  55. #You can walk an entire directory tree using os.path.walk but no use here...