Reputation: 3
I am trying to use GET through requests to get current code for a project of mine hosted on Github to have an auto update system of some sort, but whenever I GET the latest latest code (raw github file) it adds blank lines in between lines of code, for example:
def clear():
os.system('cls' if os.name =='nt' else 'clear')
clear()
becomes
def clear():
os.system('cls' if os.name =='nt' else 'clear')
clear()
It is adding unnecessary lines in between everything when their is none in the code. My code is:
import requests, time
ask = input('Would you like to update to the latest version of Switchence? ') # Switchence is the name of my app
if ask == 'y' or ask == 'yes':
print('Ok, updating...')
try:
onlineVersion = requests.get('https://raw.githubusercontent.com/Aethese/Switchence/main/main.py')
if onlineVersion.status_code != 200:
print(f'[ERROR] status code is not 200, it is {onlineVersion.status_code} so the program will not update')
time.sleep(5)
else:
onlineVersion = onlineVersion.text
with open('autoupdate.py', 'w') as file: # autoupdate.py is my test file
file.write(onlineVersion)
print('Successfully updated to latest version! Rerun the program to use the newest version!')
time.sleep(5)
except Exception as error:
print(f'Couldn\'t get latest latest update | {error}')
time.sleep(5)
elif ask == 'n' or ask == 'no':
print('Ok, you will not update to the latest version')
time.sleep(5)
else:
print('The only acceptable answers are Yes or No')
time.sleep(5)
Can someone help me or explain to me why my code won't work? I am running latest pip version of requests.
Upvotes: 0
Views: 340
Reputation: 36
Somehow opening the file in binary mode seems to fix the issue:
onlineVersion = onlineVersion.content
with open('autoupdate.py', 'wb') as file: # autoupdate.py is my test file
file.write(onlineVersion)
This has probably something to do with Python inserts both a carriage return and a line feed after variable, instead of just line feed
Upvotes: 1