1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| ''' Created on 2011-8-1
@author: Killua @E-mail:[email protected] @Description: '''
import os import time
def context_replace(file, search_for, replace_with, new_file = 'new_file'): try: os.remove(new_file) except os.error: pass
fi = open(file) fo = open(new_file, 'w')
for line in fi.readlines(): fo.write(line.replace(search_for, replace_with))
fi.close() fo.close()
def file_list(filepath): for filename in os.listdir(filepath): print(filename)
def current_word_dir(): print("Currnet Directory:" + os.getcwd())
def make_dir(dir_name): os.mkdir(dir_name)
def delete_dir(dir_name): if not os.path.isdir(dir_name): print("It's not a directory") return else: if len(os.listdir(dir_name)) == 0: os.rmdir(dir_name) else: print("The directory you want to delete is not empty,")
def get_file_info(filename): file_state = os.stat(filename) mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = file_state print("size:", size, "bytes") print("owner:", uid, gid) print("created:", time.ctime(ctime)) print("last accessed:", time.ctime(atime)) print("last modified:", time.ctime(mtime)) print("mode:", oct(mode)) print("inode/dev", ino, dev)
def os_command_excute(cmd): os_name = os.name if os_name == "nt": print("Windows Command") else: print("Unix/Linux Command") os.system(cmd)
def main(): os_command_excute("ls -l")
if __name__ == "__main__": main()
|