Write Many Files in a for Loop

Write output of for loop to multiple files

Move the 2nd with statement inside your for loop and instead of using an outside for loop to count the number of lines, use the enumerate function which returns a value AND its index:

with open('testdata.txt', 'r') as input:
for index, line in enumerate(input):
with open('filename{}.txt'.format(index), 'w') as output:
output.write(line)

Also, the use of format is typically preferred to the % string formatting syntax.

Write multiple files inside for-loop

I'm guessing you're on some sort of *nix system as the error has to do with / interpreted a part of the path.

So, you have to do something to name your files correctly or create the path you want to save the output.

Having said that, using the URL as a file name is not a great idea, because of the above error.

You could either replace the / with, say _ or just name your files differently.

Also, this:

urls = ['https://link1',
'https://link2']

Is already a list, so no need for this:

url_list = list(urls)

And there's no need for two for loops. You can write to a file as you scrape the URLS from the list.

Here's the working code with some dummy website:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup

urls = ['https://lipsum.com/', 'https://de.lipsum.com/']

for url in urls:
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(response.content, "html.parser")
page = soup.find("div", {"id": "Panes"}).find("p").getText()
with open('filename_{}.txt'.format(url.replace("/", "_")), 'w', encoding="utf8") as outfile:
outfile.write('\n'.join([i for i in page.split('\n') if len(i) > 0]))

You could also use your approach with enumerate():

import requests
from bs4 import BeautifulSoup

urls = ['https://lipsum.com/', 'https://de.lipsum.com/']

for index, url in enumerate(urls, start=1):
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(response.content, "html.parser")
page = soup.find("div", {"id": "Panes"}).find("p").getText()
with open('filename_{}.txt'.format(index), 'w', encoding="utf8") as outfile:
outfile.write('\n'.join([i for i in page.split('\n') if len(i) > 0]))

Write the outcome of a for loop in multiple files in a single file in shell

I suggest with bash:

for each in *.bam; do
data1=$(samtools view -c -F 4 "${each}")
data2=$(samtools view -c -f 4 "${each}")
echo -e "${each}\t${data1}\t${data2}"
done

Writing to a file in a for loop only writes the last value

That is because you are opening , writing and closing the file 10 times inside your for loop. Opening a file in w mode erases whatever was in the file previously, so every time you open the file, the contents written to it in previous iterations get erased.

myfile = open('xyz.txt', 'w')
myfile.writelines(var1)
myfile.close()

You should open and close your file outside for loop.

myfile = open('xyz.txt', 'w')
for line in lines:
var1, var2 = line.split(",");
myfile.write("%s\n" % var1)

myfile.close()
text_file.close()

You should also notice to use write and not writelines.

writelines writes a list of lines to your file.

Also you should check out the answers posted by folks here that uses with statement. That is the elegant way to do file read/write operations in Python

Write many files in a for loop

Given your lst, the following will write this out to a series of TXT files with names equal to the name of lst, plus .txt:

lapply(names(lst),
function(x, lst) write.table(lst[[x]], paste(x, ".txt", sep = ""),
col.names=FALSE, row.names=FALSE, sep="\t",
quote=FALSE),
lst)

To modify your for() loop, try:

for(i in seq_along(lst)) {
write.table(lst[[i]], paste(names(lst)[i], ".txt", sep = ""),
col.names = FALSE, row.names = FALSE, sep = "\t", quote = FALSE)
}

The problem was trying to or assuming R would paste together the filenames for you.

While loop to write in several text files

Guess where does input('finished? ') write the text? In the sys.stdout you just closed! Try setting it back to console

sys.stdout = open(file_txt, "w")

print('This will be printed in the file')

sys.stdout.close()
sys.stdout = sys.__stdout__


Related Topics



Leave a reply



Submit