GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — LearnPython/FileReadCopyBasic.py at master · Pragad/LearnPython Expand file tree/ Copy pathFileReadCopyBasic.pyMore file actions 114 lines (92 loc) · 2.72 KB / Copy pathFileReadCopyBasic.pyFile metadata and controls114 lines (92 loc) · 2.72 KB 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 # !/usr/bin/python import os, sys, random import shutil # https://stackoverflow.com/questions/489861/locking-a-file-in-python #from filelock import FileLock #https://pythonhosted.org/lockfile/lockfile.html from lockfile import LockFile # Open a file fd = os.open("foo.txt", os.O_RDWR) # Reading text ret = os.read(fd, 12) print ret #assert os.path.exists("foo1.txt") #with FileLock("myfile.txt"): # # work with the file as it is now locked # print("Lock acquired.") lock = LockFile("myfile.txt") with lock: print lock.path, 'is locked.' with open("foo.txt", "r") as f: f.seek(135); print f.read(6); f.seek(0, 2) eof = f.tell() print eof offset = random.randint(0, eof) numBytes = random.randint(0, eof - offset) print "Offset=%d numBytes=%d" % (offset, numBytes) f.seek(offset) print f.read(numBytes) shutil.copyfile("foo.txt", "foo.txt" + ".old") #shutil.copytree("bar", "bar" + ".old") # Generate Random dir inside a dir def __newname(): random = random.Random(seed) l = random.randint(1, 16) n = [random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") for i in xrange(l)] return "".join(n) def __newsubpath(path): while True: p = os.path.join(path, __newname()) if not os.path.exists(p): return p def __getdir_recurse(path): try: n = random.choice(os.listdir(path)) except: return path p = os.path.join(path, n) if os.path.isdir(p): return __getdir_recurse(p) else: return path def __getdir(path): p = __getdir_recurse(path).replace(path, '') print "new path:" , p parts = p.split(os.sep) print "parts1:" , parts r = random.randint(1, len(parts)) print "rand:" , r parts = parts[0:r] print "parts2:" , parts return os.path.join(path, *parts) def __getsubpath(path): try: n = random.choice(os.listdir(path)) except: return path return os.path.join(path, n) basepath = "/Users/pragad/ItsMine/Dev_Git/LearnPython/deleteThisDir" #basepath = __newsubpath(__getdir(basepath)) print __getdir(basepath) def listdir_nohidden(path): for f in os.listdir(path): if not f.startswith('.'): yield f # This get a deeper directory path def getdir_recurse(path): try: dirent = list(listdir_nohidden(path)) print dirent n = random.choice(dirent) print n except Exception as e: print e newpath = "/Users/pragad/ItsMine/Dev_Git/LearnPython" getdir_recurse(newpath) def listfiles(path): fullFilesList = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))] filesList = [f for f in fullFilesList if not f.startswith('.')] return filesList print listfiles(newpath) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |