我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用stat.S_ISREG。
def __hashEntry(self, prefix, entry, s): if stat.S_ISREG(s.st_mode): digest = self.__index.check(prefix, entry, s, hashFile) elif stat.S_ISDIR(s.st_mode): digest = self.__hashDir(prefix, entry) elif stat.S_ISLNK(s.st_mode): digest = self.__index.check(prefix, entry, s, DirHasher.__hashLink) elif stat.S_ISBLK(s.st_mode) or stat.S_ISCHR(s.st_mode): digest = struct.pack("<L", s.st_rdev) elif stat.S_ISFIFO(s.st_mode): digest = b'' else: digest = b'' logging.getLogger(__name__).warning("Unknown file: %s", entry) return digest
def __init__(self, filename): self.filename = filename try: if not stat.S_ISREG(os.stat(filename).st_mode): raise IOError("GZIP detection works only for regular files") self.f = gzip.open(filename,"rb") magic = self.f.read(4) except IOError: self.f = open(filename,"rb") magic = self.f.read(4) if magic == b"\xa1\xb2\xc3\xd4": #big endian self.endian = ">" self.reader = _RawPcapOldReader(self.f, self.endian) elif magic == b"\xd4\xc3\xb2\xa1": #little endian self.endian = "<" self.reader = _RawPcapOldReader(self.f, self.endian) elif magic == b"\x0a\x0d\x0d\x0a": #PcapNG self.reader = _RawPcapNGReader(self.f) else: raise Scapy_Exception("Not a pcap capture file (bad magic)")
def check_module_timestamps(self, module_path=False, mtime=0): extensions = ('.py', '.xml', '.csv') "Walk the tree of a module to find the last updated file" for file in os.listdir(module_path): file_path = os.path.join(module_path, file) statinfo = os.stat(file_path) file_mtime = 0 if stat.S_ISDIR(statinfo.st_mode): file_mtime = self.check_module_timestamps(file_path) elif stat.S_ISREG(statinfo.st_mode): if any(file.endswith(ex) for ex in extensions): file_mtime = statinfo.st_mtime else: raise Exception( 'Unknown file mode in module path %s' % file_path) if file_mtime > mtime: mtime = file_mtime return mtime
def findall(self): """Find all files under the base and set ``allfiles`` to the absolute pathnames of files found. """ from stat import S_ISREG, S_ISDIR, S_ISLNK self.allfiles = allfiles = [] root = self.base stack = [root] pop = stack.pop push = stack.append while stack: root = pop() names = os.listdir(root) for name in names: fullname = os.path.join(root, name) # Avoid excess stat calls -- just one will do, thank you! stat = os.stat(fullname) mode = stat.st_mode if S_ISREG(mode): allfiles.append(fsdecode(fullname)) elif S_ISDIR(mode) and not S_ISLNK(mode): push(fullname)
def unzip_file(filename, location, flatten=True): """ Unzip the file (with path `filename`) to the destination `location`. All files are written based on system defaults and umask (i.e. permissions are not preserved), except that regular file members with any execute permissions (user, group, or world) have "chmod +x" applied after being written. Note that for windows, any execute changes using os.chmod are no-ops per the python docs. """ ensure_dir(location) zipfp = open(filename, 'rb') try: zip = zipfile.ZipFile(zipfp, allowZip64=True) leading = has_leading_dir(zip.namelist()) and flatten for info in zip.infolist(): name = info.filename data = zip.read(name) fn = name if leading: fn = split_leading_dir(name)[1] fn = os.path.join(location, fn) dir = os.path.dirname(fn) if fn.endswith('/') or fn.endswith('\\'): # A directory ensure_dir(fn) else: ensure_dir(dir) fp = open(fn, 'wb') try: fp.write(data) finally: fp.close() mode = info.external_attr >> 16 # if mode and regular file and any execute permissions for # user/group/world? if mode and stat.S_ISREG(mode) and mode & 0o111: # make dest file have execute for user/group/world # (chmod +x) no-op on windows per python docs os.chmod(fn, (0o777 - current_umask() | 0o111)) finally: zipfp.close()
def isfile(path): """Test whether a path is a regular file""" try: st = os.stat(path) except OSError: return False return stat.S_ISREG(st.st_mode) # Is a path a directory? # This follows symbolic links, so both islink() and isdir() # can be true for the same path on systems that support symlinks
def _check_type(self, path): """Check the path """ s = os.stat(path) if stat.S_ISDIR(s.st_mode) or stat.S_ISREG(s.st_mode): return path raise ValueError("Invalid Charm at % %s" % ( path, "Invalid file type for a charm"))
def isfile(self): try: with self('r'): return stat.S_ISREG(self.stat().st_mode) except IOError: return False
def findall(dir = os.curdir): """Find all files under 'dir' and return the list of full filenames (relative to 'dir'). """ from stat import ST_MODE, S_ISREG, S_ISDIR, S_ISLNK list = [] stack = [dir] pop = stack.pop push = stack.append while stack: dir = pop() names = os.listdir(dir) for name in names: if dir != os.curdir: # avoid the dreaded "./" syndrome fullname = os.path.join(dir, name) else: fullname = name # Avoid excess stat calls -- just one will do, thank you! stat = os.stat(fullname) mode = stat[ST_MODE] if S_ISREG(mode): list.append(fullname) elif S_ISDIR(mode) and not S_ISLNK(mode): push(fullname) return list
def isfile(path): """Test whether a path is a regular file""" try: st = os.stat(path) except os.error: return False return stat.S_ISREG(st.st_mode) # Is a path a directory? # This follows symbolic links, so both islink() and isdir() # can be true for the same path on systems that support symlinks
def phase2(self): # Distinguish files, directories, funnies self.common_dirs = [] self.common_files = [] self.common_funny = [] for x in self.common: a_path = os.path.join(self.left, x) b_path = os.path.join(self.right, x) ok = 1 try: a_stat = os.stat(a_path) except os.error, why: # print 'Can\'t stat', a_path, ':', why[1] ok = 0 try: b_stat = os.stat(b_path) except os.error, why: # print 'Can\'t stat', b_path, ':', why[1] ok = 0 if ok: a_type = stat.S_IFMT(a_stat.st_mode) b_type = stat.S_IFMT(b_stat.st_mode) if a_type != b_type: self.common_funny.append(x) elif stat.S_ISDIR(a_type): self.common_dirs.append(x) elif stat.S_ISREG(a_type): self.common_files.append(x) else: self.common_funny.append(x) else: self.common_funny.append(x)
def update_files(self): ls = [] for name in self.listdir(): absname = os.path.realpath(os.path.join(self.folder, name)) try: st = os.stat(absname) except EnvironmentError as err: if err.errno != errno.ENOENT: raise else: if not stat.S_ISREG(st.st_mode): continue fid = self.get_file_id(st) ls.append((fid, absname)) # check existent files for fid, file in list(self._files_map.items()): try: st = os.stat(file.name) except EnvironmentError as err: if err.errno == errno.ENOENT: self.unwatch(file, fid) else: raise else: if fid != self.get_file_id(st): # same name but different file (rotation); reload it. self.unwatch(file, fid) self.watch(file.name) # add new ones for fid, fname in ls: if fid not in self._files_map: self.watch(fname)
def _remote_path_isfile(sftp, remote_path): if _remote_path_exists(sftp, remote_path): mode = sftp.stat(remote_path).st_mode if stat.S_ISREG(mode) and not stat.S_ISLNK(mode): return True return False
def _upload_file(sftp, local_path, remote_path): """Wrapper to upload a single file. Raises: paramiko.sftp.SFTPError: Internal paramiko error due to SSH connection or other issues. IOError: Local file does not exist or cannot be read; post-transfer file sanity checks (size, etc...) fail Exception: Unknown failures """ # local_path_stat = os.stat(local_path) # if not os.path.exists(local_path): # logging.error("Skipping %s. Path does not exist.") # or \ # not stat.S_ISREG(local_path_stat.st_mode): # logging.error('%s is not a regular file.' % (local_path)) # Create remote directories if necessary try: logging.info("Uploading %s to %s" % ( local_path, remote_path)) sftp.put(local_path, remote_path) except paramiko.sftp.SFTPError as e: logging.error("SFTP Error %s" % e.message) raise except IOError as e: logging.error("IOError %s" % e.message) raise except Exception: logging.error("Failure to upload %s %s" % (local_path, remote_path)) raise
def convert(self, value, param, ctx): rv = value is_dash = self.file_okay and self.allow_dash and rv in (b'-', '-') if not is_dash: if self.resolve_path: rv = os.path.realpath(rv) try: st = os.stat(rv) except OSError: if not self.exists: return self.coerce_path_result(rv) self.fail('%s "%s" does not exist.' % ( self.path_type, filename_to_ui(value) ), param, ctx) if not self.file_okay and stat.S_ISREG(st.st_mode): self.fail('%s "%s" is a file.' % ( self.path_type, filename_to_ui(value) ), param, ctx) if not self.dir_okay and stat.S_ISDIR(st.st_mode): self.fail('%s "%s" is a directory.' % ( self.path_type, filename_to_ui(value) ), param, ctx) if self.writable and not os.access(value, os.W_OK): self.fail('%s "%s" is not writable.' % ( self.path_type, filename_to_ui(value) ), param, ctx) if self.readable and not os.access(value, os.R_OK): self.fail('%s "%s" is not readable.' % ( self.path_type, filename_to_ui(value) ), param, ctx) return self.coerce_path_result(rv)
def isfile(self): return S_ISREG(self._osstatresult.st_mode)
def file(self): return S_ISREG(self._stat().mode)
def isfile(self): st = self.statinfo if not st: self.restat(False) st = self.statinfo if not st: return False return S_ISREG(st[ST_MODE])
def is_file(self, *, follow_symlinks=True): return stat.S_ISREG(self.stat(follow_symlinks).st_mode)