Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to upload files and folders with pygithub

from github import Github
g = Github("username", "password")

repo = g.get_user().get_repo(GITHUB_REPO)
all_files = []
contents = repo.get_contents("")
while contents:
    file_content = contents.pop(0)
    if file_content.type == "dir":
        contents.extend(repo.get_contents(file_content.path))
    else:
        file = file_content
        all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))

with open('/tmp/file.txt', 'r') as file:
    content = file.read()

# Upload to github
git_prefix = 'folder1/'
git_file = git_prefix + 'file.txt'
if git_file in all_files:
    contents = repo.get_contents(git_file)
    repo.update_file(contents.path, "committing files", content, contents.sha, branch="master")
    print(git_file + ' UPDATED')
else:
    repo.create_file(git_file, "committing files", content, branch="master")
    print(git_file + ' CREATED')
Comment

PREVIOUS NEXT
Code Example
Python :: class dog_years: years = 0 __ fido=Dog() fido.years=3 print(fido.dog_years()) 
Python :: arithmetic encoding python 
Python :: if the answer satisfiest the condition so how to stop it to run further ahead in python 
Python :: odoo create new admin user command line 
Python :: python arithmetic operation with list 
Python :: wand image resize 
Python :: print out python 
Python :: how to access range of tuples in python 
Python :: select series of columns 
Python :: transverse tensor in pytorch 
Python :: Python - Comment faire pour supprimer les cotes de Chaîne 
Python :: add hours to date time in python 
Python :: Constructing a Class with __init__ function 
Python :: mumtiply to matrices python 
Python :: successful=true for number in range (3) print ("Attempt") if successful: print ("Successful") breal 
Python :: python indexing 
Python :: python to open .seg file 
Python :: how to make an infinite loop in python 
Python :: python extract words from string with format 
Python :: groupby Fiscal year 
Python :: How to make bot commands case insensitive in discord.py 
Python :: how to check for non-datetime value in python 
Python :: python check if more than 1 is true 
Python :: abrir notebooks jupyter administrador de archivos 
Python :: uneven chunks of array slices 
Python :: loops with variables that count 
Python :: rolling call on one column and groupby second pandas 
Python :: create animation from sequence of image python 
Python :: ascending order in python using bubble sort 
Python :: convert c code to python code online 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =