10 examples of 'python write list of strings to file' in Python

Every line of 'python write list of strings to file' 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
this disclaimer
185def writelines(self, *args):
186 for f, line in zip(self._files, args):
187 f.write(line)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

689def write_list_of_files(file_list, output_file):
690 """
691 Save `file_list` list of files into `output_file` text file.
692 Args:
693 file_list: (list of str) of path to files
694 output_file: (str) path to the output txt file
695
696 Returns:
697 output_file
698 """
699 from os.path import isfile
700
701 assert isinstance(file_list, list), 'First argument must be a list'
702 assert isinstance(output_file, str), 'Second argument must be a str'
703 if isfile(output_file):
704 return None
705
706 text_file = open(output_file, 'w+')
707 for created_file in file_list:
708 text_file.write(created_file + '\n')
709 text_file.close()
710 return output_file
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)
802def writelines(self, list):
803 return self.__csio.writelines(list)
169def _writelines(self, obj):
170 """
171 Writes the main body of gwt-like weights files.
172 This code part is repeatedly used for many weight file formats.
173 Header lines, however, are different from format to format.
174 So, for code reusability, this part is separated out from
175 write function by Myunghwa Hwang.
176 """
177 for id in obj.id_order:
178 neighbors = list(zip(obj.neighbors[id], obj.weights[id]))
179 str_id = "_".join(str(id).split())
180 for neighbor, weight in neighbors:
181 neighbor = "_".join(str(neighbor).split())
182
183 self.file.write('%s %s %6G\n' % (str_id,
184 neighbor, weight))
185 self.pos += 1
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)
47def write_list_into_files(list_n, f):
48 for i in range(0,len(list_n),1):
49 f.write("%f " % list_n[i])
50 f.write("\n")
72def write_file(lines, dest):
73 for line in lines:
74 print(line)
75 print(line, file=dest)
63def write_files(outfile, filelist):
64 with open(outfile, "wb") as thefile:
65 for item in filelist:
66 thefile.write("%s\n" % item)
75def writecsv(filename, rows, separator="\t", encoding="utf-8-sig"):
76 """Write the rows to the file.
77
78 :param filename: (str)
79 :param rows: (list)
80 :param separator: (str):
81 :param encoding: (str):
82
83 """
84 with codecs.open(filename, "w+", encoding) as f:
85 for row in rows:
86 tmp = []
87 for s in row:
88 if isinstance(s, (float, int)):
89 s = str(s)
90 else:
91 s = '"%s"' % s
92 tmp.append(s)
93 f.write('%s\n' % separator.join(tmp))

Related snippets