/ Published in: Python
Shows basic filehandling with python.
Expand |
Embed | Plain Text
#!/usr/bin/env python import sys,os from time import * # This is my second pythonscript. # Its intention is to get used to fileaccess. # This program doesn't serve a certain purpose. # @author: [email protected] def launch(): print "Current working Directory: ", os.getcwd() print "Contents: ", os.listdir(os.getcwd()) #chdir('foldername') change into folder "foldername" #mkdir('name') create folder "name" #mkdirs('name/subname/subsubname') #rmdir('name') removes if empty print "Operating System: ", os.name print "Current directory: ", os.curdir print "Parent directory: ", os.pardir #path.isdir('path') true if path is directory #path.isfile('filename') true if filename is file #path.exists('path') true if exists filehandle = file('test.dat', 'w+') #modi: r,w,a,r+,w+,a+, #modi: rb,wb,ab (binary) rU, aU (Universal Newline n) #filehandle.read() read everything from filehandle #filehandle.readline() read one line #filehandle.readlines() read everything linewisely #filehandle.close() close file handle #filehandle.write() write string to file handle #filehandle.writelines() filehandle.write("test") filehandle.close() #remove('filename') removes file #rename('filename_orig', 'filename_new') #filehandle.mode gets mod (rwx) #filehandle.closed true if closed #Utility-Funktionen #seek(int) set filepointer position #tell() get filepointer position #Aufruf launch()
You need to login to post a comment.
