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.
Related
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.
So I am making a python game and everything in the python programs is fine except for the writing part
Here is a part of GameFile2.py:
class lostToss():
def botChoose(self):
print("Bot is choosing between batting and bowling.")
for dot in range(5):
print((dot + 1) * ".")
time.sleep(0.25)
for x in range(10):
print("")
choices = ['Bat', 'Bowl']
botting = random.choice(choices)
print(f"And the bot has chose to {botting}")
time.sleep(1)
if botting == "Bat":
f = open("GamePoints.txt","w")
with open('GamePoints.txt, 'a+') as f:
f.write('52L05yt0smdwPMA4wgdTUF7Yh4dLT')
print('ok')
time.sleep(10)
import GameFile3
elif botting == "Bowl":
file = open("GamePoints.txt","w+")
with open('GamePoints.txt', 'a+') as file:
file.write('69L05yt0smdwPMA4wgdLOL7Yh4dLT')
time.sleep(2)
import GameFile3
The problem is in the 15th and 22nd line, I ran the file many times and the "ok" text
was printed but the code couldn't be written in the file.
Can anyone help?
Omit the file opening before the with statements. Like this
f = open("GamePoints.txt","w") # remove this line
with open('GamePoints.txt', 'a+') as f:
file = open("GamePoints.txt","w+") # remove this line
with open('GamePoints.txt', 'a+') as file:
In the first block, you are missing the closing quote after .txt.
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")
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')
I'm obviously still a beginner, but I'm creating a parsing file that goes through a text file, and builds a file that I need.
The logistics aren't as important as what's obviously happening.
import fileinput;
for lines in fileinput.FileInput("c:/manhattan.txt", inplace=1):
lines = lines.strip();
if lines == '': continue;
print(lines);
source = open('c:/manhattan.txt','r');
hrt = open('c:/test.hrt','w');
currentline = str(source.readline());
currentline.lstrip();
workingline = '';
while currentline[0] != " ":
if currentline[0].isdigit() == True:
if currentline[0:3] == workingline[0:3] and len(workingline)<160:
workingline = workingline + currentline[4:7];
currentline = str(source.readline());
currentline.lstrip();
else:
hrt.write(('\x01' + 'LOC01LOC' + workingline).ljust(401,' ') +'\n');
workingline = currentline[0:7].replace(';','E');
currentline = str(source.readline());
currentline.lstrip();
else:
currentline = str(source.readline());
currentline.lstrip();
hrt.write(('\x01'+'LOC50US1 A*').ljust(401,' ' +'\n');
hrt.write(('\x02'+'LOCSUNSAT00:0023:5960 60 99990.00 0.00').ljust(401,' ')+'\n');
hrt.write(('\x02'+'US SUNSAT00:0023:5960 60 99990.03 0.03').ljust(401,' ') +'\n');
hrt.close();
source.close();
It works fine in the python command line, but when running the .py file it does't write the last three lines to the file.
Any ideas?
Have you tried flushing the buffer before you close the file?
hrt.flush()