def int_to_size_str(size,b1024=True,zero='0 B',less_than_zero='%s', ): '''Convert a file size to human-readable form. Keyword arguments: size -- file size in bytes b1024 -- if True (default), use multiples of 1024 if False, use multiples of 1000 Returns: string test git ''' U,T,N,F=py.importUTNF() if py.istr(size) or py.isbyte(size): size=U.len(size) size=py.int(size) if size < 0: if py.callable(less_than_zero): return less_than_zero(size) if py.istr(less_than_zero): if '%' in less_than_zero: return less_than_zero%size if '{' in less_than_zero and '}' in less_than_zero: return less_than_zero.format(size) return less_than_zero if size==0:return zero # raise ValueError('number must be non-negative') multiple = 1024.0 if b1024 else 1000.0 #another if statement style if size<=multiple:return py.str(size)+' B' for suffix in SUFFIXES[multiple]: size /= multiple if size < multiple: return '{0:.3f} {1}'.format(size, suffix) raise ValueError('number too large')
def set_title(title, h=0): '''在python内设置的,退出后 会还原 py3 : SetWindowTextW ''' if not py.istr(title): title = py.str(title) if not h: h = getCmdHandle() if py.is3(): return user32.SetWindowTextW(h, title) else: return user32.SetWindowTextA(h, title)
def isDir(ast): '''#TODO: ''' if not py.istr(ast):ast=py.str(ast) if not ast:return False ast=ast.replace('\\','/') if exist(ast): return _p.isdir(ast) else: if ast.endswith('/'):return True else: return False
def __str__(self): U=py.importU() s='<{}'.format(numToSize(self) ) repr=U.get_duplicated_kargs(self.ka,'repr','str','s','st','__repr__','__str__',no_pop=True) if repr: if py.callable(repr): return repr(self, **self.ka ) else: return py.str(repr) return T.justify(s,**self.ka)+'>'
def writeIterable(file,data,end='\n',overwrite=True,encoding=None): U=py.importU() if not encoding:encoding=U.encoding file=autoPath(file) if overwrite:new(file) if py.is2():f=py.open(file,'a') else: f=py.open(file,'a',encoding=encoding) for i in data: f.write(py.str(i)+end) f.close() return f.name
def makeDirs(ap,isFile=False,cd=0,no_auto_path=False): ''' 访问移动硬盘时,可能出现已经创建成功,但是 F.ls 看不到的情况。 用explorer访问后又正常 ''' U=py.importU() if not no_auto_path:ap=autoPath(ap) if not py.isbool(isFile) and not py.isint(isFile): U.log('F.md(str,isFile={})'.format(repr(isFile))) if py.is3(): from pathlib import Path p=Path(ap).absolute() if isFile: return makeDirs(p.parent,isFile=False) if p.is_file():#if not exists, is_dir() is_file() both return False return py.No(ap+' exists , and it is a file') r=py.No('unexpected err') try: p.mkdir() except (FileNotFoundError,) as e: if p.parent==p:# 'D:\\' 驱动器不存在 r=e else: r=makeDirs(p.parent,isFile=False) if r:p.mkdir() # 建立父目录后,再次尝试创建本目录 else:return r # else:return r #但是如果父目录本来是个文件,再mkdir则FileNotFoundError: [WinError 3] 系统找不到指定的路径。 except FileExistsError: pass except Exception as e: r=e if p.exists(): if not no_auto_path:sp=autoPath(p) else:sp=py.str(p.absolute()).replace('\\','/') if p.is_dir() and not sp.endswith('/'): sp+='/' r=sp else: r=py.No(r,p) if r and cd:U.cd(r) return r # if py.is2(): ######################################### U=py.importU() sp=getSplitor(ap) ap=ap.split(sp) if isFile:ap=ap[:-1] # if not isabs(ap): # if not ap.startswith('.'): # if ap.startswith(sp):ap=U.gst[:-1]+ap # else:ap=U.gst+ap # else: base='' for i in ap: base+=(i+sp) #TODO 2019-1-19 未处理存在的是文件 而不是 文件夹的情况 if exist(base):continue else: try: _os.mkdir(base) except Exception as e: if U.iswin(): if e.winerror==5:continue#WindowsError(5, '') 拒绝访问 if e.winerror==183:continue#WindowsError 183 当文件已存在时,无法创建该文件。 if 'exists' in e.args[1]:#(17, 'File exists') cygwin; continue # U.repl(printCount=True) setErr(e) return False return True