Replace old file with better one or discard new wasteful file.
432766
def _cleanup_after_optimize_aux(filename, new_filename, old_format,
                                new_format):
    
    bytes_in = 0
    bytes_out = 0
    final_filename = filename
    try:
        bytes_in = os.stat(filename).st_size
        bytes_out = os.stat(new_filename).st_size
        if (bytes_out > 0) and ((bytes_out < bytes_in) or Settings.bigger):
            if old_format != new_format:
                final_filename = replace_ext(filename,
                                             new_format.lower())
            rem_filename = filename + REMOVE_EXT
            if not Settings.test:
                os.rename(filename, rem_filename)
                os.rename(new_filename, final_filename)
                os.remove(rem_filename)
            else:
                os.remove(new_filename)

        else:
            os.remove(new_filename)
            bytes_out = bytes_in
    except OSError as ex:
        print(ex)

    return final_filename, bytes_in, bytes_out
54802
def rename_file(old, new, path=None):
    
    cwd = os.getcwd()
    try:
        if path:
            os.chdir(path)
    except OSError:
        raise
    try:
        os.rename(old, new)
        os.chdir(cwd)
        return True
    except OSError:
        os.chdir(cwd)
        raise
1054195
def rmfile(path):
    """"""
    if osp.isfile(path):
        if is_win:
            os.chmod(path, 0o777)
        os.remove(path)
432319
def copy(self, new_path, replace=False):
        """"""
        if replace or not get_file(new_path).exists():
            self.key.copy(self.key.bucket, new_path)
            return True
        return False
47382
def _update_file(self, seek_to_end=True):
        """"""
        try:
            self.close()
            self._file = self.open()
        except IOError:
            pass
        else:
            if not self._file:
                return

            self.active = True
            try:
                st = os.stat(self._filename)
            except EnvironmentError, err:
                if err.errno == errno.ENOENT:
                    self._log_info('file removed')
                    self.close()

            fid = self.get_file_id(st)
            if not self._fid:
                self._fid = fid

            if fid != self._fid:
                self._log_info('file rotated')
                self.close()
            elif seek_to_end:
                self._seek_to_end()
175126
def move_file_to_file(old_path, new_path):
        
        try:
            os.rename(old_path, new_path)
        except:
            old_file = os.path.basename(old_path)
            target_directory, target_file = os.path.dirname(
                os.path.abspath(new_path)), os.path.basename(new_path)
            Document.move_file_to_directory(
                old_path,
                target_directory
            )  # move old file to new directory, change name to new name
            os.rename(os.path.join(target_directory, old_file),
                      os.path.join(target_directory, target_file))
14655
def rollback(self):
        """"""
        for p in self._moves:
            logging.info("Moving to %s\n from %s", *p)

        for new_path, path in self._moves:
            try:
                logger.debug('Replacing %s from %s', new_path, path)
                if os.path.isfile(new_path):
                    os.unlink(new_path)
                elif os.path.isdir(new_path):
                    rmtree(new_path)
                renames(path, new_path)
            except OSError as ex:
                logger.error("Failed to restore %s", new_path)
                logger.debug("Exception: %s", ex)

        self.commit()
868943
def replace(self, infile):
        '''
        '''
        gf = infile[31:]
        same_size_index = []
        while len(same_size_index) <= 1:
            index = random.randint(0,len(gf)-1)
            index_len = len(gf[index])
            same_size_index = [i for (i,g) in enumerate(gf) if len(g) == index_len]
        else:
            same_size_index = random.choice(same_size_index[:])
        gf[index], gf[same_size_index] = gf[same_size_index], gf[index]
        return infile[:31] + gf
1684
def delete(self):
        """"""
        if self.isfile:
            os.remove(self.fn)
        elif self.isdir:
            shutil.rmtree(self.fn)
656633
public function replaceFile($old, $new, array $content)
    {
        $oldFileContent = file_get_contents($old);
        file_put_contents($new, vsprintf($oldFileContent, $content));
        unlink($old);
    }
628177
def _apply_replacement(error, found_file, file_lines):
    """"""
    fixed_lines = file_lines
    fixed_lines[error[1].line - 1] = error[1].replacement
    concatenated_fixed_lines = "".join(fixed_lines)

    # Only fix one error at a time
    found_file.seek(0)
    found_file.write(concatenated_fixed_lines)
    found_file.truncate()
490029
def _copy_replace(src, dst, replacements):
    """"""
    with src.open() as infile, dst.open('w') as outfile:
        outfile.write(re.sub(
            '|'.join(re.escape(k) for k in replacements),
            lambda m: str(replacements[m.group(0)]),
            infile.read()
        ))
534099
def _overwrite(self, n):
        
        if os.path.isfile(n[:-4]):
            shutil.copy2(n[:-4], n[:-4] + ".old")
            print("Old file {0} saved as {1}.old".format(
                n[:-4].split("/")[-1], n[:-4].split("/")[-1]))
        if os.path.isfile(n):
            shutil.move(n, n[:-4])
            print("New file {0} overwrite as {1}".format(
                n.split("/")[-1], n[:-4].split("/")[-1]))
302780
def update(oldrev=nil, newrev=nil)
      if index_file_exists? && oldrev != "0"*40
        hg([
            "log --debug -r ?:? --style ? >> ?",
            oldrev, newrev, Style.file_index, path
          ])
      else
        hg(["log --debug -r : --style ? > ?", Style.file_index, path])
      end
    end
1209623
def replace_in_files(search, replace, depth=0, paths=None, confirm=True):
    

    # have the user select some files
    if paths==None:
        paths = _s.dialogs.MultipleFiles('DIS AND DAT|*.*')
    if paths == []: return

    for path in paths:
        lines = read_lines(path)

        if depth: N=min(len(lines),depth)
        else:     N=len(lines)

        for n in range(0,N):
            if lines[n].find(search) >= 0:
                lines[n] = lines[n].replace(search,replace)
                print(path.split(_os.path.pathsep)[-1]+ ': "'+lines[n]+'"')

        # only write if we're not confirming
        if not confirm:
            _os.rename(path, path+".backup")
            write_to_file(path, join(lines, ''))

    if confirm:
        if input("yes? ")=="yes":
            replace_in_files(search,replace,depth,paths,False)

    return
1227059
def cleanup():
    
    script_dir = os.path.dirname(__file__) if not os.path.islink(__file__) else os.path.dirname(os.readlink(__file__))
    script_dir = os.path.realpath(os.path.join(script_dir, ".."))
    shutil.rmtree(os.path.join(script_dir, "dist"), ignore_errors=True)
    shutil.rmtree(os.path.join(script_dir, "tmp"), ignore_errors=True)
    shutil.rmtree(os.path.join(script_dir, "urh.egg-info"), ignore_errors=True)
    shutil.rmtree(os.path.join(script_dir, "src", "urh.egg-info"), ignore_errors=True)
    shutil.rmtree(os.path.join(script_dir, "src", "urh", "tmp"), ignore_errors=True)
1292535
function skip() {
    rimraf(file, function() {})
    delete f.versions[v]
    if (f._attachments)
      delete f._attachments[file]
    need.splice(need.indexOf(v), 1)
    maybeDone(null)
  }