...
This commit is contained in:
parent
820737bc69
commit
9d3b7827eb
4 changed files with 152 additions and 1 deletions
15
MyObsidianVault/Knowledgebase/Python/Writing a textfile.md
Normal file
15
MyObsidianVault/Knowledgebase/Python/Writing a textfile.md
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
|
||||
```python
|
||||
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
|
||||
with open('output.txt', 'w') as f:
|
||||
f.writelines(lines) # Writes all lines at once [1][3]
|
||||
|
||||
# Alternative with join()
|
||||
with open('output.txt', 'w') as f:
|
||||
f.write('\n'.join(lines)) # Joins with newlines automatically [1][4]
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue