10 examples of 'python append to file new line' in Python

Every line of 'python append to file new line' 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
120def write(self, line):
121 if self.expandtabs:
122 self._write(line.expandtabs(self.tabsize))
123 else:
124 self._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

114def add_line(self, line):
115 self._add(line)
65def writelines(self, lines):
66 """ Log a series of lines excluding trailing whitespace """
67
68 for line in lines:
69 self.write(line)
70 return 1
113def prepend_to_file(fn, text):
114 with open(fn, 'r') as f:
115 body = f.readlines()
116
117 with open(fn, 'w') as f:
118 f.write(text)
119 f.writelines(body)
74def append(self, line):
75 line.script = self
76 self._lines.append(line)
185def writelines(self, *args):
186 for f, line in zip(self._files, args):
187 f.write(line)
308def write_new_line(line):
309 f.write('\\\n' + ' ' * 4 + line + ' ')
21def insert_in_file(file_name, text, add_text):
22 with open(file_name) as f:
23 new_text=f.read()
24 if text not in new_text:
25 print '"{}" was not found in file "{}"!'.format(text, file_name)
26 return
27 new_text = re.sub(r'^(%s)' % re.escape(text), r'\1\n%s' % add_text, new_text, flags=re.MULTILINE)
28
29 with open(file_name, "w") as f:
30 f.write(new_text)
31
32 return
165def insertInFile(file, index, value) :
166 f = open(file, "r");
167 contents = f.readlines();
168 f.close();
169
170 contents.insert(index, value);
171
172 f = open(file, "w");
173 contents = "".join(contents);
174 f.write(contents);
175 f.close();
104def write(self, data):
105 prefix = self.tabs() if self.start else ''
106 self.buf.append(prefix + data)
107 self.start = False

Related snippets