Saving text file in a for loop - python

I'm trying to loop through a file, strip the sentences into individual lines, and then export that data.
filename = '00000BF8_ar.txt'
with open(filename, mode="r") as outfile:
str_output = outfile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
for s in sentenceSplit:
print(s.strip() + ".")
#output += s
myfile = open(filename, 'w')
myfile.writelines(s)
myfile.close()
Unfortunately, it looks like the loop only goes through a few lines and saves them. So the whole file isn't looped through and saved. Any help on how I can fix that?

Here is the code I hope this is what you want to achieve,
filename = '00000BF8_ar.txt'
with open(filename, mode="r") as outfile:
str_output = outfile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
l=[]
for s in sentenceSplit:
l.append(s.strip() + ".")
myfile = open(filename, 'w')
myfile.write('\n'.join(l))
myfile.close()

Each time you re-open the file with the 'w' option, you basically erase its content.
Try modifying your code like this:
filename = '00000BF8_ar.txt'
with open(filename, "r") as infile:
str_output = infile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
with open(filename, "w") as outfile:
for s in sentenceSplit:
print(s.strip() + ".")
#output += s
s.writelines(s)
Another way to achieve the same thing would have been to open a new file using open(filename_new, 'a') which open a file for appending, but as a rule of thumb try not to open/close files inside a loop.

open(filename, 'w') will overwrite the file every time it starts. My guess is that what's currently happening is that only the last element in sentenceSplit is showing up in myfile.
The simple "solution" is to use append instead of write:
open(filename, 'a')
which will simply start writing at the end of the file, without deleting the rest of it.
However, as #chepner's comment states, why are you reopening the file at all? I would recommend changing your code to this:
with open(filename, mode="r") as outfile:
str_output = outfile.readlines()
str_output = ''.join(str_output)
sentenceSplit = filter(None, str_output.split("."))
with open(filename, mode='w') as myfile:
for s in sentenceSplit:
print(s.strip() + ".")
myfile.writelines(s)
This way, instead of opening it many times, and overwriting it every time, you're only opening it once and just writing to it continuously.

Related

Go through files in given directory with python, read each file line by line and remove first and last string in the line and save updated file

So I have some .txt files inside of directory. Each .txt file contains some paths like:
'C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module.c'
'C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module2.c'
'C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module3.c'
I need just some small function that will go through each line of each file inside of a dir and remove there ', so only clear path is left like:
C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module.c
C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module2.c
C:\d\folder\project\folder\Folder1\Folder2\Folder3\Module3.c
My code at the moment is:
for filename in files:
with open(filename, 'r') as file:
content = file.read().split('\n')
for line in content:
if line.startswith('')and line.endswith(''):
remove('')
Please assist!
SOLUTION:
I have managed to find a solution with a bit different approach:
for filename in files:
f = open(filename, 'rt')
filedata = f.read()
filedata = filedata.replace("'","")
f.close()
f = open(filename, 'wt')
f.write(filedata)
f.close()
Thanks!
python has a hirarchy to strings ', ", "" and so on so you can wrap a uptick into quotes for a split. Since we have the first element '' before the tick the second is your path
line.split("'")[1]
Edit: If i understood you correctly you want this
for filename in files:
paths = []
with open(filename, 'r') as file:
content = file.read().split('\n')
for line in content:
paths.append(line.split("'")[1])
file.close()
with open(filename, 'w') as file:
file.writelines(paths)
file.close()
Soo I just did bit different approach and managed to find a solution:
for filename in files:
f = open(filename, 'rt')
filedata = f.read()
filedata = filedata.replace("'","")
f.close()
f = open(filename, 'wt')
f.write(filedata)
f.close()
Thanks guys anyway!

How to open an input file, edit it, then save it with a different name with python

I would like to open a text file named file1.txt, count the length, then close it with the name file2.txt.
I very much prefer not importing anything.
file1 = open('file1.txt')
def wordCount(file1):
contents = file1.read()
file1.close()
print(contents)
return len(contents)
print(wordCount(file1))
It comes out as it should but I have no idea where begin the next step of the process.
# Copying then editing files
local_saved = []
with open("file1.txt", "r") as f:
local_saved.append(f.read())
with open("file2.txt", "w+") as f:
text_to_write = local_saved[0]
# Edit text_to_write below.
# Can be deleted (text_to_write = "") or changed in any way as if it were a normal string.
# ...
f.write(text_to_write)
Here it is:
file1 = open('file1.txt')
def wordCount(file1):
contents = file1.read()
file2=open('file2.txt','w')
file2.write(contents)
file2.close()
file1.close()
print(contents)
return len(contents)
print(wordCount(file1))

Why is it only writing last input to txt?

Output:
Sorry, this was being awfully awkward when I trying to paste my Python code into the code box on this forum post.
Code:
# update three quotes to a file
file_name = "my_quote.txt"
# create a file called my_quote.txt
new_file = open(file_name, 'w')
new_file.close()
def update_file(file_name, quote):
# First open the file
new_file = open(file_name, 'w')
new_file.write("This is an update\n")
new_file.write(quote)
new_file.write("\n\n")
# now close the file
new_file.close()
for index in range(3):
quote = input("Enter your favorite quote: ")
update_file(file_name, quote)
# Now print the contents to the screen
new_file = open(file_name, 'r')
print(new_file.read())
# And finally close the file
new_file.close(
You should be using append instead of write. When you use write, it creates a new file regardless of what was there before. Try new_file = open(file_name, 'a')
Why is it only writing last input to txt?
Everytime you do open(file_name, 'w') it clears the contents of the file and begins to write from the start of the file.
If you would like to append new content to that file do
open(file_name, 'a')
I guess you should use a instead of w to append to file:
new_file = open(file_name, 'a')
And read the docs before asking of course ;)

Python script that will go through folders and replace content in them [duplicate]

Currently I'm using this:
f = open(filename, 'r+')
text = f.read()
text = re.sub('foobar', 'bar', text)
f.seek(0)
f.write(text)
f.close()
But the problem is that the old file is larger than the new file. So I end up with a new file that has a part of the old file on the end of it.
If you don't want to close and reopen the file, to avoid race conditions, you could truncate it:
f = open(filename, 'r+')
text = f.read()
text = re.sub('foobar', 'bar', text)
f.seek(0)
f.write(text)
f.truncate()
f.close()
The functionality will likely also be cleaner and safer using open as a context manager, which will close the file handler, even if an error occurs!
with open(filename, 'r+') as f:
text = f.read()
text = re.sub('foobar', 'bar', text)
f.seek(0)
f.write(text)
f.truncate()
The fileinput module has an inplace mode for writing changes to the file you are processing without using temporary files etc. The module nicely encapsulates the common operation of looping over the lines in a list of files, via an object which transparently keeps track of the file name, line number etc if you should want to inspect them inside the loop.
from fileinput import FileInput
for line in FileInput("file", inplace=1):
line = line.replace("foobar", "bar")
print(line)
Probably it would be easier and neater to close the file after text = re.sub('foobar', 'bar', text), re-open it for writing (thus clearing old contents), and write your updated text to it.
I find it easier to remember to just read it and then write it.
For example:
with open('file') as f:
data = f.read()
with open('file', 'w') as f:
f.write('hello')
To anyone who wants to read and overwrite by line, refer to this answer.
https://stackoverflow.com/a/71285415/11442980
filename = input("Enter filename: ")
with open(filename, 'r+') as file:
lines = file.readlines()
file.seek(0)
for line in lines:
value = int(line)
file.write(str(value + 1))
file.truncate()
Honestly you can take a look at this class that I built which does basic file operations. The write method overwrites and append keeps old data.
class IO:
def read(self, filename):
toRead = open(filename, "rb")
out = toRead.read()
toRead.close()
return out
def write(self, filename, data):
toWrite = open(filename, "wb")
out = toWrite.write(data)
toWrite.close()
def append(self, filename, data):
append = self.read(filename)
self.write(filename, append+data)
Try writing it in a new file..
f = open(filename, 'r+')
f2= open(filename2,'a+')
text = f.read()
text = re.sub('foobar', 'bar', text)
f.seek(0)
f.close()
f2.write(text)
fw.close()

Python, adding a file into another file

I have to define a function: save_file(filename, new_list) which takes a file name and a new list and writes that list to the file in the correct format.
So, for example,
save_file(’file.txt’, load_file(’file.txt’))
(load_file is a predefined function which opens and reads the file)
should overwrite the new list with exactly the same content.
I have no clue how to go about this, any ideas?
The load_file function seems to work but can't seem to get the save_file function working.
This is what I have so far:
I have this so far:
def load_file(filename):
f = open(filename, 'Ur')
for line in f:
print line
f.close()
def save_file(filename, new_list):
with open(new_list, 'Ur') as f1:
with open(filename, 'w') as f2:
f2.write(f1.read())
Since new_list is clearly a list of lines, not a filename, you don't need all the stuff with opening and reading it. And you also can't do saving in a single write.
But you can do it almost that simply.
You didn't specify whether the lines in new_list still have their newlines. Let's first assume they do. So, all you have to do is:
def save_file(filename, new_list):
with open(filename, 'w') as f:
f.write(''.join(new_list))
… or …:
def save_file(filename, new_list):
with open(filename, 'w') as f:
f.writelines(new_list)
But your teacher may be expecting something like this:
def save_file(filename, new_list):
with open(filename, 'w') as f:
for line in new_list:
f.write(line)
What if the newlines were stripped off, so we have to add them back? Then things are a bit more complicated the first two ways, but still very easy the third way:
def save_file(filename, new_list):
with open(filename, 'w') as f:
f.write('\n'.join(new_list) + '\n')
def save_file(filename, new_list):
with open(filename, 'w') as f:
f.writelines(line + '\n' for line in new_list)
def save_file(filename, new_list):
with open(filename, 'w') as f:
for line in new_list:
f.write(line + '\n')
Meanwhile, you have not gotten load_file to work. It's supposed to return a list of lines, but it doesn't return anything (or, rather, it returns None). printing something just prints it out for the user to see, it doesn't store anything for later use.
You want something like this:
def load_file(filename):
lines = []
with open(filename, 'Ur') as f:
for line in f:
lines.append(line)
return lines
However, there's a much simpler way to write this. If you can do for line in f:, then f is some kind of iterable. It's almost the same thing as a list—and if you want to make it into an actual list, that's trivial:
def load_file(filename):
with open(filename, 'Ur') as f:
return list(f)
def save_file(filename, new_list):
with open(new_list, 'r') as a:
with open(filename, 'w') as b:
b.write(a.read())
Just a small adjustment to SaltChicken's answer.
Use print >> to make it simply :
>>> with open('/src/file', 'r') as f1, open('/dst/file', 'w') as f2:
... print >> f2, f1.read()
Inspired from What does this code mean: "print >> sys.stderr".

Categories