10 examples of 'write string to file python' in Python

Every line of 'write string to file 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.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
251def write_string_to_file(filename, file_content):
252 """Writes a string to a given file.
253
254 Args:
255 filename: string, path to a file
256 file_content: string, contents that need to be written to the file
257
258 Raises:
259 errors.OpError: If there are errors during the operation.
260 """
261 with FileIO(filename, mode="w") as f:
262 f.write(file_content)
56def write_to_file(interpreter, pseudo_self, parameters):
57 data_obj = parameters[0]
58 assert isinstance(data_obj, PrimitiveStrObject)
59 assert isinstance(pseudo_self, PrimitiveFileObject)
60
61 pseudo_self.value.write(data_obj.value)
62
63 return pseudo_self
40def 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)
163def writelines(self,string,overwrite=False):
164 """
165 Writes to the file whose name is being stored
166 @ In, string or list of string, the string to write to the file
167 @ In, overwrite, bool (optional), if true will open file in write mode instead of append
168 @ Out, None
169 """
170 if not self.isOpen(): self.open('a' if not overwrite else 'w')
171 self.__file.writelines(string)
188def 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
260def write_data_to_file(content, file_path):
261 """Writes data to file."""
262 with open(file_path, 'wb') as file_handle:
263 file_handle.write(str(content))
27def 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
289def write_text_to_file(filepath, text):
290 with io.open(filepath, 'w', encoding='utf-8') as f:
291 f.write(text)
41def write_to_file(out_file, line):
42 out_file.write(line.encode('utf8') + '\n')
94def WriteFile(filepath, content):
95 cfile = open(filepath,"w")
96 cfile.write(content)
97 cfile.close()

Related snippets