我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用os.renames()。
def unpack(src_dir, dst_dir): '''Move everything under `src_dir` to `dst_dir`, and delete the former.''' for dirpath, dirnames, filenames in os.walk(src_dir): subdir = os.path.relpath(dirpath, src_dir) for f in filenames: src = os.path.join(dirpath, f) dst = os.path.join(dst_dir, subdir, f) os.renames(src, dst) for n, d in reversed(list(enumerate(dirnames))): src = os.path.join(dirpath, d) dst = os.path.join(dst_dir, subdir, d) if not os.path.exists(dst): # Directory does not exist in destination, # rename it and prune it from os.walk list. os.renames(src, dst) del dirnames[n] # Cleanup. for dirpath, dirnames, filenames in os.walk(src_dir, topdown=True): assert not filenames os.rmdir(dirpath)
def renames(old, new): """Like os.renames(), but handles renaming across devices.""" # Implementation borrowed from os.renames(). head, tail = os.path.split(new) if head and tail and not os.path.exists(head): os.makedirs(head) shutil.move(old, new) head, tail = os.path.split(old) if head and tail: try: os.removedirs(head) except OSError: pass
def rename(self, pattern, dryrun=False): newname = pattern.format(**self.headers) newfp = os.path.join(os.path.dirname(self.filename), newname) print("Rename: '%s' -> '%s'" % (self.filename, newfp), file=sys.stderr) if not dryrun: os.renames(self.filename, newfp)
def reset(job): main_file = job.files[0] job_full_path = main_file.filepath if os.path.exists(job_full_path + ".bak"): os.remove(job_full_path) # repathed file os.renames(job_full_path + ".bak", job_full_path)
def testFile(conn, job_id, slave_id, rfile, job_prefix, main_path=None): job_full_path = createLocalPath(rfile, job_prefix, main_path, rfile.force) found = os.path.exists(job_full_path) if found and rfile.signature is not None: found_signature = hashFile(job_full_path) found = found_signature == rfile.signature if not found: print("Found file %s at %s but signature mismatch!" % (rfile.filepath, job_full_path)) os.remove(job_full_path) if not found: # Force prefix path if not found job_full_path = createLocalPath(rfile, job_prefix, main_path, True) print("Downloading", job_full_path) temp_path = os.path.join(job_prefix, "slave.temp") with ConnectionContext(): conn.request("GET", fileURL(job_id, rfile.index), headers={"slave-id":slave_id}) response = conn.getresponse() if response.status != http.client.OK: return None # file for job not returned by server, need to return an error code to server f = open(temp_path, "wb") buf = response.read(1024) while buf: f.write(buf) buf = response.read(1024) f.close() os.renames(temp_path, job_full_path) rfile.filepath = job_full_path return job_full_path
def renames(self, new): """ .. seealso:: :func:`os.renames` """ os.renames(self, new) return self._next_class(new) # # --- Create/delete operations on directories