I have a weird problem when reading a file named ReadingFile. So what I'm trying to do is read everything that's inside a file named ReadingFile and right after reading a line, remove that line that was read in the ReadingFile.
The code:
import os
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler('Test.log')
file_formatter = logging.Formatter('[%(asctime)s]: %(message)s')
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
def RemoveOneLine(file, ToDelete):
with open(file, 'r') as f:
lines = f.readlines()
with open(file, 'w') as f:
for line in lines:
if line.strip('\n') != ToDelete:
f.write(line)
def Test(filename):
with open(filename, 'r') as a_file:
for line in a_file:
test_line = line.strip()
logger.info('was [{}]'.format(test_line))
RemoveOneLine(filename, test_line)
Test('ReadingFile')
The ReadingFile content, I will upload on Pastebin because it's too big. https://pastebin.com/9kTwv5Kj what's this big list? It's a list of accounts, don't worry.. they are public keys.. no harm can be done knowing the keys.
The problem.
After some time I. get truncated data and wrong data, wrong public keys.. too long public keys or too short, or same lenght keys but non existent in ReadingFile..
As you can see in pastebine the first line is GA25ETO3HJFO4NJLM2PG25WGHWMX35DS4KI7BLY2PA5DTNCDUN2UUSVP when loop reach line GAL7MUG4G3MMO24JKESAMWWIAJT4L7TKX74EONZSB7RKDUGAYSWUQ7JG it starts to get truncated data..
Compared screens, left non changed, right truncated..
What i need is just: Read file -> Read Line -> Remove Line that was read -> Read again first line -> Close program when file is empty.. maybe i did in wrong way, im pretty new to python.
What's wrong?
Try this one. The code is corrected like this:
Adding close() method, each time when finished the file processing.
Adding a missed readlines() method in the Test() function.
[Code]
import os
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
#You can clear log file eatch time using mode='w'
#OR you can use mode='a' to Append it
file_handler = logging.FileHandler('Test.log', mode='w')
file_formatter = logging.Formatter('[%(asctime)s]: %(message)s')
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
def RemoveOneLine(file, ToDelete):
with open(file, 'r') as f:
lines = f.readlines()
f.close() # we can now close the file after readlines()
with open(file, 'w') as f:
for line in lines:
if line.strip('\n') != ToDelete:
f.write(line)
f.close() # we can close the file now after we have update it
def Test(filename):
with open(filename, 'r') as a_file:
lines_a_file = a_file.readlines() #should add readlines() here
a_file.close() # we can now close the file after readlines()
for line in lines_a_file:
test_line = line.strip()
logger.info('was [{}]'.format(test_line))
RemoveOneLine(filename, test_line)
Test('ReadingFile')
Related
iam using this code to to pull the first line at text file at threading mod before delete it from the file
with open(r'C:\datanames\names.txt','r') as fin:
name = fin.readline()
with open(r'C:\datanames\names.txt', 'r') as fin:
data = fin.read().splitlines(True)
with open(r'C:\datanames\names.txt', 'w') as fout:
fout.writelines(data[1:])
put it make me lose the data Often
Is there a more efficient and practical way to use it in such a situation? (threading)
I see no reason to use threading for this. It's very straightforward.
To remove the first line from a file do this:
FILENAME = 'foo.txt'
with open(FILENAME, 'r+') as file:
lines = file.readlines()
file.seek(0)
file.writelines(lines[1:])
file.truncate()
I can't close this file, as the file is directly fed into the 'lines'-list.
I have tried closing with command lines.close() but it doesn't work.
def readfile():
lines = [line.rstrip('\n') for line in open('8ballresponses.txt', 'r')]
print(random.choice(lines))
I don't get an error, but i want to be able to close the file.
Instead of file object, lines is a list , so you can't close it. And you should store file object open('8ballresponses.txt', 'r') with a variable for closing it later:
def readfile(file_path):
test_file = open(file_path, 'r')
lines = [line.rstrip('\n') for line in test_file]
test_file.close()
print(random.choice(lines))
Or simply use with "to close a file in python, without a file object":
def readfile(file_path):
with open(file_path, 'r') as test_file:
lines = [line.rstrip('\n') for line in test_file]
print(lines)
you can use with open command. this will automatically handle all the test cases failure etc. (inbuild try except and finally in python)
below is example similiar to your code
import random
def readfile():
lines = []
with open(r"C:\Users\user\Desktop\test\read.txt",'r') as f:
lines = f.readlines()
print(random.choice(lines))
You can use try and finally block to do the work.
For example :
def readfile():
file = open('8ballresponses.txt', 'r')
try:
lines = [line.rstrip('\n') for line in file]
print(random.choice(lines))
finally:
file.close()
In this post
"When the with ends, the file will be closed. This is true even if an exception is raised inside of it."
You manually invoke the close() method of a file object explicitly or implicitly by leaving a with open(...): block. This works of course always and on any Python implementation.
Use with this will close implicitly after the block is complete
with open('8ballresponses.txt', 'r') as file:
lines = [ line.rstrip("\n") for line in file ]
Could someone give me some guidance on how you would get the contents of your text file on my python code without opening up the text file in another window?
Just point me in the right direction on how I should do it (No need for solutions)
with open(workfile, 'r') as f:
for line in f:
print line
If you don't use the context manager (the with statement) you will need to explicitly call f.close(), for example:
f = open('workfile', 'r')
line = f.readline()
print line
f.close()
file = open("your_file.txt", "r")
file.read()
I have to read in a file, change a sections of the text here and there, and then write out to the same file.
Currently I do:
f = open(file)
file_str = f.read() # read it in as a string, Not line by line
f.close()
#
# do_actions_on_file_str
#
f = open(file, 'w') # to clear the file
f.write(file_str)
f.close()
But I would imagine that there is a more pythonic approach that yields the same result.
Suggestions?
That looks straightforward, and clear already. Any suggestion depends on how big the files are. If not really huge that looks fine. If really large, you could process in chunks.
But you could use a context manager, to avoid the explicit closes.
with open(filename) as f:
file_str = f.read()
# do stuff with file_str
with open(filename, "w") as f:
f.write(file_str)
If you work line by line you can use fileinput with inplace mode
import fileinput
for line in fileinput.input(mifile, inplace=1):
print process(line)
if you need to process all the text at once, then your code can be optimized a bit using with that takes care of closing the file:
with open(myfile) as f:
file_str = f.read()
#
do_actions_on_file_str
#
with open(myfile, 'w') as f:
f.write(file_str)
I have an XML file that contains an illegal character, I am iterating through the file, removing the character from all of the lines and storing the lines in a list. I now want to write those same lines back into the file and overwrite what is already there.
I tried this:
file = open(filename, "r+")
#do stuff
Which is only appending the results to the end of the file, I would like to overwrite the existing file.
And this:
file = open(filename, "r")
#read from the file
file.close()
file = open(filename, "w")
#write to file
file.close()
This gives me a Bad File Descriptor error.
How can i read and write to the same file?
Thanks
You could re-write the lines list with writelines function.
with open(filename, "r") as f:
lines = f.readlines()
#edit lines here
with open(filename, "w") as f:
f.writelines(lines)
The reason you're appending to the end of the file the whole time is that you need to seek to the beginning of the file to write your lines out.
with open(filename, "r+") as file:
lines = file.readlines()
lines = [line.replace(bad_character, '') for line in lines]
file.seek(0)
file.writelines(lines)
file.truncate() # Will get rid of any excess characters left at the end of the file due to the length of your new file being shorter than the old one, as you've removed characters.
(Decided to just use the context manager syntax myself.)