I have some code below that counts the number of lines in decoded GitHub binary content and then looks for percent change based on the changes count of a file. This is contained in a loop within a loop in an if/else statement. What I have now works, but it outputs the results of each individual file in the pull request. I would like to write the if/else just once if any of the results in the set of returned files meet the condition in the if statement (else print no file has changed) and then move on to the next set for evaluation.
found = False
for data in repo.pull_request(prs.number).files():
if data.filename.endswith((".png",".jpeg",".gif")):
pass
else:
for files_content in [repo.blob(data.sha)]:
binary_coded_content = io.BytesIO((base64.b64decode(files_content.content)))
tempfile = 'temp'
with open(tempfile,'wb') as f:
f.write(binary_coded_content.read())
num_lines = sum(1 for line in open(tempfile, encoding='utf8') if line.rstrip())
if data.changes_count/num_lines > 0.25:
found = True
break
if found:
print("A file has changed by more than 25%", '\n')
else:
print("No file has changed by more than 25%", '\n')
If I understand correctly you want to create a found variable and test it outside the loop like this
found = False # <----
for data in repo.pull_request(prs.number).files():
if data.filename.endswith((".png",".jpeg",".gif")):
pass
else:
for files_content in [repo.blob(data.sha)]:
binary_coded_content = io.BytesIO((base64.b64decode(files_content.content)))
tempfile = 'temp'
with open(tempfile,'wb') as f:
f.write(binary_coded_content.read())
num_lines = sum(1 for line in open(tempfile, encoding='utf8') if line.rstrip())
if data.changes_count/num_lines > 0.25:
found = True # <----
break #<--- break inner loop
if found:
break #<--- break outer loop
if found: # after the loop ended, we check if we found something...
print("A file has changed by more than 25%")
else:
print("No file has changed by more than 25%")
Related
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 have a file called serial.dll. The content of this file is another file's name:
a-2ED1-7156.dll
I also have 1 file called a-2ED1-7156.dll in the same directory.
When I try to check if the file exists by reading its name from serial.dll:
f = open('serial.dll', 'r')
serials = f.read()
if os.path.exists(serials):
print("ok")
else:
print("no")
Always results "no".
but:
file = 'a-2ED1-7156.dll'
if os.path.exists(file):
print("ok")
else:
print("no")
Always gives the correct result.
How can I check if the file a-2ED1-7156.dll exists by reading it from the serial.dll file?
Update Try:
f = open('serial.dll', 'r')
lines = f.readline()
for line in lines:
if os.path.exists(line):
print('ok')
else:
print("no")
results error:
no
no
no
no
no
no
no
no
no
no
no
ok
no
no
no
no
Supossing each file is in a separate line, you coud use
lines = f.readlines()
for line in lines:
if os.path.exists(line):
print('ok')
Or print only if all files exist, depending on what you want exactly.
Your problem is that lines in a file might end with the new-line character. File names usually don't have that character... For example, right now you're checking if the file a-2ED1-7156.dll\n exists - which is not. You simply need to strip() the lines before checking them as files:
f = open('serial.dll')
for line in f:
filename = line.strip()
if os.path.exists(filename):
print(f"{filename} exists")
else:
print(f"{filename} doesn't exist")
I,m a extreme noobie...
I making a dowsing program.
I have code that randomly picks a image file from a directory. (I can do this)
i need to know how to write the file path of the image to a txt file. (simple database)
Then next time read the txt file to see if that file has been selected in the last 100 entries, if it has been selected, how to make it go back to the random module and try again until it gets one that has yet to be selected in the 100 times.
Thanks
sample
os.chdir('C:\landscapes\pics')
left1 = random.choice(os.listdir("C:\landscapes\pics"))
# TEST FILE
print(left1)
os.chdir('C:\landscapes')
logfile = open('test.txt', 'r')
loglist = logfile.readlines()
logfile.close()
found = False
for line in loglist:
if str(left1) in line:
print ("Found it")
found = True
if not found:
logfile = open('test.txt', 'a')
logfile.write(str(left1)+"\n")
logfile.close()
print ("Not Found!")
I,m able to tell if the file is found or not.
I,m just at a loss of what to do next, I think I need kind of While loop?
You don't need a while loop. Instead, this can be achieved with self referencing methods, which create a sort-of, infinite loop, until a certain condition is met (i.e.: found = False). Also, I took out the references to os.chdir as you don't need those if you specify the directory you are attempting to search in the path of os.listdir, and open().
def choose_random_file():
return random.choice(os.listdir("C:\landscapes\pics"))
def validate_randomness( random_file ):
logfile = open('C:\landscapes\test.txt', 'r')
loglist = logfile.readlines()
logfile.close()
found = False
for line in loglist:
if str( random_file ) in line:
print ("Found it")
found = True
# we found the file, now break out of the for loop
break
# Check if we found the file
if found:
# If we found the file name, then circle-back to pick another file
random_file = choose_random_file()
# Now validate that the new pick is in the test.txt file again
validate_randomness( random_file )
if not found:
logfile = open('test.txt', 'a')
logfile.write(str( random_file )+"\n")
logfile.close()
print ("Not Found!")
random_file = choose_random_file()
validate_randomness( random_file )
Hope this helps point you in the right direction. Let me know if something doesn't work.
Using this Python code I get printed lines of file in UPPERCASE but file remains unchanged (lowercase.)
def open_f():
while True:
fname=raw_input("Enter filename:")
if fname != "done":
try:
fhand=open(fname, "r+")
break
except:
print "WRONG!!!"
continue
else: exit()
return fhand
fhand=open_f()
for line in fhand:
ss=line.upper().strip()
print ss
fhand.write(ss)
fhand.close()
Can you suggest please why files remain unaffected?
Code:
def file_reader(read_from_file):
with open(read_from_file, 'r') as f:
return f.read()
def file_writer(read_from_file, write_to_file):
with open(write_to_file, 'w') as f:
f.write(file_reader(read_from_file))
Usage:
Create a file named example.txt with the following content:
Hi my name is Dmitrii Gangan.
Create an empty file called file_to_be_written_to.txt
Add this as the last line file_writer("example.txt", "file_to_be_written_to.txt") of your .py python file.
python <your_python_script.py> from the terminal.
NOTE: They all must be in the same folder.
Result:
file_to_be_written_to.txt:
Hi my name is Dmitrii Gangan.
This program should do as you requested and allows for modifying the file as it is being read. Each line is read, converted to uppercase, and then written back to the source file. Since it runs on a line-by-line basis, the most extra memory it should need would be related to the length of the longest line.
Example 1
def main():
with get_file('Enter filename: ') as file:
while True:
position = file.tell() # remember beginning of line
line = file.readline() # get the next available line
if not line: # check if at end of the file
break # program is finished at EOF
file.seek(position) # go back to the line's start
file.write(line.upper()) # write the line in uppercase
def get_file(prompt):
while True:
try: # run and catch any error
return open(input(prompt), 'r+t') # r+t = read, write, text
except EOFError: # see if user if finished
raise SystemExit() # exit the program if so
except OSError as error: # check for file problems
print(error) # report operation errors
if __name__ == '__main__':
main()
The following is similar to what you see up above but works in binary mode instead of text mode. Instead of operating on lines, it processes the file in chunks based on the given BUFFER_SIZE and can operate more efficiently. The code under the main loop may replace the code in the loop if you wish for the program to check that it is operating correctly. The assert statements check some assumptions.
Example 2
BUFFER_SIZE = 1 << 20
def main():
with get_file('Enter filename: ') as file:
while True:
position = file.tell()
buffer = file.read(BUFFER_SIZE)
if not buffer:
return
file.seek(position)
file.write(buffer.upper())
# The following code will not run but can replace the code in the loop.
start = file.tell()
buffer = file.read(BUFFER_SIZE)
if not buffer:
return
stop = file.tell()
assert file.seek(start) == start
assert file.write(buffer.upper()) == len(buffer)
assert file.tell() == stop
def get_file(prompt):
while True:
try:
return open(input(prompt), 'r+b')
except EOFError:
raise SystemExit()
except OSError as error:
print(error)
if __name__ == '__main__':
main()
I suggest the following approach:
1) Read/close the file, return the filename and content
2) Create a new file with above filename, and content with UPPERCASE
def open_f():
while True:
fname=raw_input("Enter filename:")
if fname != "done":
try:
with open(fname, "r+") as fhand:
ss = fhand.read()
break
except:
print "WRONG!!!"
continue
else: exit()
return fname, ss
fname, ss =open_f()
with open(fname, "w+") as fhand:
fhand.write(ss.upper())
Like already alluded to in comments, you cannot successively read from and write to the same file -- the first write will truncate the file, so you cannot read anything more from the handle at that point.
Fortunately, the fileinput module offers a convenient inplace mode which works exactly like you want.
import fileinput
for line in fileinput.input(somefilename, inplace=True):
print(line.upper().strip())