does someone know why after i decrypt file it breaks? - python

import os
def encrypt(filename,extension):
file = open(filename+'.'+extension,'r',encoding="Latin-1")
red = str(file.read())
final = [extension+'Q']
fin = ''
for x in range(len(red) - 1):
if red[x] == '0':
final.append('#!#')
else:
final.append(red[x])
for x in final:
fin += x
file.close()
os.system('del '+filename+'.'+extension)
file = open(filename,'a')
file.close()
file = open(filename,'w',encoding="Latin-1")
file.write(fin)
file.close()
def decrypt(filename):
ignore = 0
file = open(filename,'r',encoding="Latin-1")
dat = str(file.read())
final = []
fin = ''
exten = ''
is_done = False
for x in range(len(dat) - 1):
if dat[x] == '#' and dat[x + 1] == '!' and dat[x + 2] == '#' and ignore == 0:
final.append('0')
ignore = 2
elif ignore == 0 and is_done == True:
final.append(dat[x])
elif not ignore < 1:
ignore -= 1
if dat[x] == 'Q':
is_done = True
if is_done == False:
exten += dat[x]
for x in final:
fin += x
print(filename)
print(exten)
file.close()
file = open(filename+'.'+exten,'a')
file.close()
file = open(filename+'.'+exten,'w',encoding="Latin-1")
file.write(str(fin))
file.close()
os.system('del '+filename)
decrypt('eee')
#encrypt('eee','png')
#print(open('test.docx','r',encoding="Latin-1").readlines())
#print(open('eee.png','r',encoding='latin-1').read())
i just want it to work as i expect to work and i dont have idea what breaks it help btw when i open files with notepad it looks same so idk whats wrong at all thats weird , sorry for that my code is messy XD thats how my every code looks like

What do you mean by it 'breaks'? What I'm seeing in your code is:
When you run open(filename,...), you assume file is correctly holding the file object. You have no error-checking in either encrypt() or decrypt(). After those lines, read the documentation for the function and see what it returns. You should be able to do something in the lines of:
try:
file = open(filename,'r',encoding="Latin-1")
except err:
...
# start file operations here.
File handling can and will go wrong at some point. You can give the program any file that you can specify, but you need to make sure that operation will run successfully. Common file errors are it not existing, being already open, or the permissions being wrong
In both encrypt() and decrypt() you repeat this block:
os.system('del '+filename+'.'+extension)
file = open(filename,'a')
file.close()
file = open(filename,'w',encoding="Latin-1")
file.write(fin)
file.close()
where you tell the system to DELETE your file and then re-open it. I suspect that is a part of why you have issues with the file. I'd also consider removing the second close() & open() calls to see if you can change the mode without manipulating the file pointer. If this block of code has to be in both parts, separate it into its own function. And run error handling on Everything.

Related

Python: Separating txt file to multiple files using a reoccuring symbol

I have a .txt file of amino acids separated by ">node" like this:
Filename.txt :
>NODE_1
MSETLVLTRPDDWHVHLRDGAALQSVVPYTARQFARAIAMPNLKPPITTAEQAQAYRERI
KFFLGTDSAPHASVMKENSVCGAGCFTALSALELYAEAFEAAGALDKLEAFASFHGADFY
GLPRNTTQVTLRKTEWTLPESVPFGEAAQLKPLRGGEALRWKLD*
>NODE_2
MSTWHKVQGRPKAQARRPGRKSKDDFVTRVEHDAKNDALLQLVRAEWAMLRSDIATFRGD
MVERFGKVEGEITGIKGQIDGLKGEMQGVKGEVEGLRGSLTTTQWVVGTAMALLAVVTQV
PSIISAYRFPPAGSSAFPAPGSLPTVPGSPASAASAP*
I want to separate this file into two (or as many as there are nodes) files;
Filename1.txt :
>NODE
MSETLVLTRPDDWHVHLRDGAALQSVVPYTARQFARAIAMPNLKPPITTAEQAQAYRERI
KFFLGTDSAPHASVMKENSVCGAGCFTALSALELYAEAFEAAGALDKLEAFASFHGADFY
GLPRNTTQVTLRKTEWTLPESVPFGEAAQLKPLRGGEALRWKLD*
Filename2.txt :
>NODE
MSTWHKVQGRPKAQARRPGRKSKDDFVTRVEHDAKNDALLQLVRAEWAMLRSDIATFRGD
MVERFGKVEGEITGIKGQIDGLKGEMQGVKGEVEGLRGSLTTTQWVVGTAMALLAVVTQV
PSIISAYRFPPAGSSAFPAPGSLPTVPGSPASAASAP*
With a number after the filename
This code works, however it deletes the ">NODE" line and does not create a file for the last node (the one without a '>' afterwards).
with open('FilePathway') as fo:
op = ''
start = 0
cntr = 1
for x in fo.read().split("\n"):
if x.startswith('>'):
if start == 1:
with open (str(cntr) + '.fasta','w') as opf:
opf.write(op)
opf.close()
op = ''
cntr += 1
else:
start = 1
else:
if op == '':
op = x
else:
op = op + '\n' + x
fo.close()
I canĀ“t seem to find the mistake. Would be thankful if you could point it out to me.
Thank you for your help!
Hi again! Thank you for all the comments. With your help, I managed to get it to work perfectly. For anyone with similar problems, this is my final code:
import os
import glob
folder_path = 'FilePathway'
for filename in glob.glob(os.path.join(folder_path, '*.fasta')):
with open(filename) as fo:
for line in fo.readlines():
if line.startswith('>'):
original = line
content = [original]
fileno = 1
filename = filename
y = filename.replace(".fasta","_")
def writefasta():
global content, fileno
if len(content) > 1:
with open(f'{y}{fileno}.fasta', 'w') as fout:
fout.write(''.join(content))
content = [line]
fileno += 1
with open('FilePathway') as fin:
for line in fin:
if line.startswith('>NODE'):
writefasta()
else:
content.append(line)
writefasta()
You could do it like this:
def writefasta(d):
if len(d['content']) > 1:
with open(f'Filename{d["fileno"]}.fasta', 'w') as fout:
fout.write(''.join(d['content']))
d['content'] = ['>NODE\n']
d['fileno'] += 1
with open('test.fasta') as fin:
D = {'content': ['>NODE\n'], 'fileno': 1}
for line in fin:
if line.startswith('>NODE'):
writefasta(D)
else:
D['content'].append(line)
writefasta(D)
This would be better way. It is going to write only on odd iterations. So that, ">NODE" will be skipped and files will be created only for the real content.
with open('filename.txt') as fo:
cntr=1
for i,content in enumerate(fo.read().split("\n")):
if i%2 == 1:
with open (str(cntr) + '.txt','w') as opf:
opf.write(content)
cntr += 1
By the way, since you are using context manager, you dont need to close the file.
Context managers allow you to allocate and release resources precisely
when you want to. It opens the file, writes some data to it and then
closes it.
Please check: https://book.pythontips.com/en/latest/context_managers.html
with open('FileName') as fo:
cntr = 1
for line in fo.readlines():
with open (f'{str(cntr)}.fasta','w') as opf:
opf.write(line)
opf.close()
op = ''
cntr += 1
fo.close()

Python: Writing to file using while loop fails with no errors given

I am attempting to collect only certain type of data from one file. After that the data is to be saved to another file. The function for writing for some reason is not saving to the file. The code is below:
def reading(data):
file = open("model.txt", 'r')
while (True):
line = file.readline().rstrip("\n")
if (len(line) == 0):
break
elif (line.isdigit()):
print("Number '" + line + "' is present. Adding")
file.close()
return None
def writing(data):
file = open("results.txt", 'w')
while(True):
line = somelines
if line == "0":
file.close()
break
else:
file.write(line + '\n')
return None
file = "model.txt"
data = file
somelines = reading(data)
writing(data)
I trying several things, the one above produced a TypeError (unsupported operand). Changing to str(somelines) did solve the error, but still nothing was written. I am rather confused about this. Is it the wrong definition of the "line" in the writing function? Or something else?
See this line in your writing function:
file.write(line + '\n')
where you have
line = somelines
and outside the function you have
somelines = reading(data)
You made your reading function return None. You cannot concat None with any string, hence the error.
Assuming you want one reading function which scans the input file for digits, and one writing file which writes these digits to a file until the digit read is 0, this may help:
def reading(file_name):
with open(file_name, 'r') as file:
while True:
line = file.readline().rstrip("\n")
if len(line) == 0:
break
elif line.isdigit():
print("Number '" + line + "' is present. Adding")
yield line
def writing(results_file, input_file):
file = open(results_file, 'w')
digits = reading(input_file)
for digit in digits:
if digit == "0":
file.close()
return
else:
file.write(digit + '\n')
file.close()
writing("results.txt", "model.txt")

Extract IP addresses from text file without using REGEX

I am trying to extract IPv4 addresses from a text file and save them as a list to a new file, however, I can not use regex to parse the file, Instead, I have check the characters individually. Not really sure where to start with that, everything I find seems to have import re as the first line.
So far this is what I have,
#Opens and prints wireShark txt file
fileObject = open("wireShark.txt", "r")
data = fileObject.read()
print(data)
#Save IP adresses to new file
with open('wireShark.txt') as fin, open('IPAdressess.txt', 'wt') as fout:
list(fout.write(line) for line in fin if line.rstrip())
#Opens and prints IPAdressess txt file
fileObject = open("IPAdressess.txt", "r")
data = fileObject.read()
print(data)
#Close Files
fin.close()
fout.close()
So I open the file, and I have created the file that I will put the extracted IP's in, I just don't know ow to pull them without using REGEX.
Thanks for the help.
Here is a possible solution.
The function find_first_digit, position the index at the next digit in the text if any and return True. Else return False
The functions get_dot and get_num read a number/dot and, lets the index at the position just after the number/dot and return the number/dot as str. If one of those functions fails to get the number/dot raise an MissMatch exception.
In the main loop, find a digit, save the index and then try to get an ip.
If sucess, write it to output file.
If any of the called functions raises a MissMatch exception, set the current index to the saved index plus one and start over.
class MissMatch(Exception):pass
INPUT_FILE_NAME = 'text'
OUTPUT_FILE_NAME = 'ip_list'
def find_first_digit():
while True:
c = input_file.read(1)
if not c: # EOF found!
return False
elif c.isdigit():
input_file.seek(input_file.tell() - 1)
return True
def get_num():
num = input_file.read(1) # 1st digit
if not num.isdigit():
raise MissMatch
if num != '0':
for i in range(2): # 2nd 3th digits
c = input_file.read(1)
if c.isdigit():
num += c
else:
input_file.seek(input_file.tell() - 1)
break
return num
def get_dot():
if input_file.read(1) == '.':
return '.'
else:
raise MissMatch
with open(INPUT_FILE_NAME) as input_file, open(OUTPUT_FILE_NAME, 'w') as output_file:
while True:
ip = ''
if not find_first_digit():
break
saved_position = input_file.tell()
try:
ip = get_num() + get_dot() \
+ get_num() + get_dot() \
+ get_num() + get_dot() \
+ get_num()
except MissMatch:
input_file.seek(saved_position + 1)
else:
output_file.write(ip + '\n')

Function to get content of a bash variable

I have files where bash string variables are gradually appended:
URI += "path \
path \
path \
"
<some other code>
#URI += "path"
URI += "path \
path"
As you may notice there are different way of appendings, partly over several lines. There is other code as well in those files.
Now I tried to write a function which gets the content of the variables (everything between the quotes):
def grepVar(filepath, var):
list = []
with open(filepath, "r") as file:
for num, line in enumerate(file, 1):
if var in line:
if line.count('"') is 2:
list.append(line)
# until here it works for "URIs" over 1 line
else:
num = num + 1
while(line.count('"') is 0):
list.append(line)
num = num + 1
return list
print grepVar(path, "URI")
So In the else condition I try to raise the loop manually and append all lines until another quote would appear (while-loop). I am not sure if I can tie on this idea or if I have to discard it completely. In this case could you pls give me hints how to solve my problems? I am not sure if I described it well since its kind of specific.
As line if given through a higher level for num, line in enumerate(file, 1): loop, you cannot use a while (line...) inside that loop.
A common way to solve this problem is to save state between lines. You function could become (I removed num management because I could not understand the requirement):
def grepVar(filepath, var):
lst = []
inquote = False
with open(filepath, "r") as fil:
for num, line in enumerate(fil, 1):
if inquote:
lst.append(line)
if line.count('"') > 0:
inquote = False
elif var in line:
if line.count('"') == 2:
lst.append(line)
else:
lst.append(line)
inquote = True
return lst
You should also avoid to use standard Python words such as list of file for your own variables, because the hide the standard meanings.

How to make python continually save file io

I have the following code,
def main():
SucessCount = 0
line = []
StringList = ''
url = "https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?key=&match_id=1957663499"
http = urllib3.PoolManager()
for i in range(1860878309, 1860878309 + 99999999999999999 ):
with open("DotaResults.txt", "w") as file:
if SucessCount > 70000:
break
result = http.request('GET', 'https://api.steampowered.com/IDOTA2Match_570/GetMatchDetails/V001/?key=F878695BB3657028B92BCA60CEA03E16&match_id=' + str(i))
x = json.loads(result.data.decode('utf-8'))
if('error' in x['result']):
print("Not found")
else:
if validityCheck(x['result']['players']) == True and x['result']['game_mode'] == 22:
line = vectorList(x)
#print(line.count(1))
if(line.count(1) == 10 or line.count(1) == 11):
SucessCount += 1
print('Ok = ' + str(SucessCount))
print("MatchId = " + str(x['result']['match_id']))
StringList = ','.join([str(i) for i in line])
file.write(StringList + '\n')
if(SucessCount % 5 == 0):
file.flush()
file.close()
time.sleep(30)
The problem I am having is that when I press the stop button in pycharm(in normal running mode)nothing shows up in the file, even tho I am closing the file every time I loop. Does anybody know why or what i can do to fix this?
You need to make four changes:
Open the file before entering the for loop -- that is, swap the with statement and the for statement.
Open the file in "a" mode instead of "w" mode. "a" stands for "append". Right now, every time you reopen the file it erases everything you wrote to it before.
Call file.flush() immediately after file.write() (that is, before the if SucessCount % 5 == 0.)
Don't close the file before sleeping.
Incidentally, the word "success" is spelled with two Cs, and in Python you do not have to put parentheses around the controlling expression of an if statement.

Categories

Resources