This tag refers to the process of writing text or data to a file.
Questions tagged [file-writing]
932 questions
425
votes
16 answers
How do I specify new lines in a string in order to write multiple lines to a file?
How can I indicate a newline in a string in Python, so that I can write multiple lines to a text file?

FabianCook
- 20,269
- 16
- 67
- 115
290
votes
15 answers
How to redirect 'print' output to a file?
I want to redirect the print to a .txt file using Python. I have a for loop, which will print the output for each of my .bam file while I want to redirect all output to one file. So I tried to put:
f = open('output.txt','w')
sys.stdout = f
at the…

LookIntoEast
- 8,048
- 18
- 64
- 92
147
votes
11 answers
Writing a dictionary to a text file?
I have a dictionary and am trying to write it to a file.
exDict = {1:1, 2:2, 3:3}
with open('file.txt', 'r') as file:
file.write(exDict)
I then have the error
file.write(exDict)
TypeError: must be str, not dict
So I fixed that error but…

Nic
- 1,549
- 2
- 13
- 13
70
votes
8 answers
How do I write to a file in Kotlin?
I can't seem to find this question yet, but what is the simplest, most-idiomatic way of opening/creating a file, writing to it, and then closing it? Looking at the kotlin.io reference and the Java documentation I managed to get this:
fun write() {
…

yiwei
- 4,022
- 9
- 36
- 54
39
votes
3 answers
Python writelines() and write() huge time difference
I was working on a script which reading a folder of files(each of size ranging from 20 MB to 100 MB), modifies some data in each line, and writes back to a copy of the file.
with open(inputPath, 'r+') as myRead:
my_list = myRead.readlines()
…

Arjun Balgovind
- 596
- 1
- 5
- 13
29
votes
4 answers
How to write the resulting RDD to a csv file in Spark python
I have a resulting RDD labelsAndPredictions = testData.map(lambda lp: lp.label).zip(predictions). This has output in this format:
[(0.0, 0.08482142857142858), (0.0, 0.11442786069651742),.....]
What I want is to create a CSV file with one column for…

Jason Donnald
- 2,256
- 9
- 36
- 49
27
votes
2 answers
How to remove \n and \r from a string
I currently am trying to get the code from this website: http://netherkingdom.netai.net/pycake.html
Then I have a python script parse out all code in html div tags, and finally write the text from between the div tags to a file. The problem is it…

HittmanA
- 293
- 1
- 4
- 11
21
votes
1 answer
Atomic writing to file on linux
Is there a way to dump a buffer to file atomically?
By "atomically" I mean: if for example someone terminates my application during writing, I'd like to have file in either before- or after-writing state, but not in a corrupted intermediate…

mambo_sun
- 509
- 4
- 13
19
votes
7 answers
Write to a file with sudo privileges in Python
The following code throws an error if it's run by a non-root user for a file owned by root, even when the non-root user has sudo privileges:
try:
f = open(filename, "w+")
except IOError:
sys.stderr.write('Error: Failed to open file %s' %…

user2824889
- 1,085
- 5
- 16
- 28
17
votes
3 answers
How to create a CSV file if it does not exist and then only append to it Python
I want to know how to create a file if it does not exist in the directory. I want to only append data.
I am getting this error in Python: No such file or directory.
This is my code:
with open (saveAddr+".csv",'a') as allpckts: …

alphiii
- 1,597
- 3
- 21
- 27
16
votes
6 answers
How to convert a PDF from base64 string to a file?
I have a PDF as a base64 string and I need to write it to file using Python.
I tried this:
import base64
base64String = "data:application/pdf;base64,JVBERi0xLjQKJeHp69MKMSAwIG9iago8PC9Qcm9kdWNlciAoU2tpYS9..."
with open('temp.pdf', 'wb') as…

Rafael Miller
- 305
- 1
- 2
- 13
16
votes
2 answers
Writing data to file in Dockerfile
I have a shell script, script.sh, that writes some lines to a file:
#!/usr/bin/env bash
printf "blah
blah
blah
blah\n" | sudo tee file.txt
Now in my Dockerfile, I add this script and run it, then attempt to add the generated file.txt:
ADD…

pcsram
- 597
- 2
- 5
- 12
13
votes
1 answer
How to bulk write TFRecords?
I have a CSV with approximately 40 million rows. Each row is a training instance. As per the documentation on consuming TFRecords I am trying to encode and save the data in a TFRecord file.
All the examples I have found (even the ones in the…

Insectatorious
- 1,305
- 3
- 14
- 29
13
votes
3 answers
Get text and rewrite(update) file contents in Python
I'm trying my hand at this rosalind problem and am running into an issue. I believe everything in my code is correct but it obviously isn't as it's not running as intended. i want to delete the contents of the file and then write some text to that…

Tare Gaskin
- 1,209
- 2
- 10
- 13
11
votes
1 answer
Writing tab separated values in Go
I'm trying to write tab separated values in a file using the tabwriter package in Go.
records map[string] []string
file, err := os.OpenFile(some_file, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
w :=…

user3502035
- 263
- 1
- 3
- 7