Home > Net >  How Can I Retrieve a Python Script From GitHub
How Can I Retrieve a Python Script From GitHub

Time:01-25

I have this Python program and it should retrive the contents of a Python script I have stored in GitHub and then write it to a text document but instead it does nothing.

I've looked at other content on StackOverflow plus, I did some research on the internet but nothing worked.

And that is stuff like:

import requests
from os import getcwd

url = "https://raw.githubusercontent.com/AngusAU293/Installer-Test-Files/main/Hello.txt"
directory = getcwd()
filename = directory   'Hello.txt'
r = requests.get(url)

f = open(filename,'w')
f.write(r.content)

CodePudding user response:

When you are trying to open the file with open function you are using the directory variable but you didn't concatenate it with the filename 'Hello.txt'. Also, you need to close the file after you write the content to it.

This code works fine for me

import requests
from os import getcwd

url = "https://raw.githubusercontent.com/AngusAU293/Installer-Test-Files/main/Hello.txt"
directory = getcwd()
filename = directory   '/Hello.txt'
r = requests.get(url)

with open(filename,'w') as f:
    f.write(r.content.decode())
  • Related