Every line of 'how to overwrite a file in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
188 def WriteFile(path, contents): 189 """ 190 Safely attempt to write data to a file, 191 replacing the existing file or creating it and 192 ensuring file is always closed at exit. 193 Return the exception object. 194 The error is None if the data was written. 195 Log results to stderr. 196 """ 197 error = None 198 with opened_w_error(path, 'w+') as (F, error): 199 if error: 200 print("Exception opening file " + path + " Error Code: " + str(error.errno) + " Error: " + error.message + error.strerror, file=sys.stderr) 201 LG().Log('ERROR', "Exception opening file " + path + " Error Code: " + str(error.errno) + " Error: " + error.message + error.strerror) 202 else: 203 F.write(contents) 204 return error
27 def write_file(name, data): 28 """ Write a file. """ 29 try: 30 with open(name, 'w', encoding='utf-8') as f: 31 # write the data 32 if sys.version_info.major == 2: 33 f.write(data.decode('utf-8')) 34 else: 35 f.write(data) 36 except IOError as e: 37 (errno, strerror) = e.args 38 sys.stderr.write('Failed to write file ' + name + ': ' + strerror) 39 raise
73 def write_file(name: str, text: str, opts: Any) -> None: 74 """Write the output file for module/package .""" 75 fname = path.join(opts.destdir, '%s.%s' % (name, opts.suffix)) 76 if opts.dryrun: 77 print(__('Would create file %s.') % fname) 78 return 79 if not opts.force and path.isfile(fname): 80 print(__('File %s already exists, skipping.') % fname) 81 else: 82 print(__('Creating file %s.') % fname) 83 with FileAvoidWrite(fname) as f: 84 f.write(text)
40 def write_new_text_file(file_path: pathlib.Path, 41 contents: str): 42 """ 43 Fails if the file already exists. 44 """ 45 with file_path.open('x') as f: 46 f.write(contents)
54 def writefile(filename,content,lvprt=1): 55 # content can be either a string or a list of strings 56 try: 57 f=open(filename,'w') 58 if isinstance(content,list): 59 for line in content: 60 f.write(line) 61 elif isinstance(content,str): 62 f.write(content) 63 else: 64 print 'Content %s cannot be written to file!' % (content) 65 f.close() 66 if lvprt>=1: 67 print 'File %s written.'%filename 68 except IOError: 69 print 'Could not write to file %s!' % (filename) 70 sys.exit(13)
55 def write(s): 56 if not opts.dry_run: 57 fp.write(s)
262 def write_file(self, what, filename, data): 263 """Write `data` to `filename` (if not a dry run) after announcing it 264 265 `what` is used in a log message to identify what is being written 266 to the file. 267 """ 268 log.info("writing %s to %s", what, filename) 269 if six.PY3: 270 data = data.encode("utf-8") 271 if not self.dry_run: 272 f = open(filename, 'wb') 273 f.write(data) 274 f.close()