0

I am trying this solution however I have a dataframe to push via commit

The code:

    df = pd.read_csv(r"\predictions_server.csv")

# Moving run_time column to the front of df
df = df[['run_time'] + [col for col in df.columns if col != 'run_time']]

# Rounding score numbers to 2 decimal places
cols = ['predicted_home_score', 'predicted_away_score', 'predicted_total_score', 'predicted_score_difference']
df[cols] = df[cols].round(2)


# list files to upload and desired file names with which you want to save on GitHub
file_list = [df]
file_names = ['predictions_server.csv']

# Specify commit message
commit_message = 'Test Python'

# Saving to GitHub
user = "cardchase"
access_token = "access token"  # Access Token is valid for 90 days. Generate new one when it expires (25 Feb 2023)
# using an access token; password does not work
g = Github(user, access_token)

# Get list of repos
print("List of Repos on Github under card_chase account:")
for repo in g.get_user().get_repos():
    print(repo.name)
    repo.edit(has_wiki=False)
print("\n")
# Getting the repo we want to use
repo = g.get_user().get_repo('Soccer-Predictor')  # repo name

# Check files under the selected repo
x = repo.get_contents("")
for labels in x:
    print(labels)
x = repo.get_contents("predictions_server.csv")  # read a specific file from your repo

# Get available branches in your repo
x = repo.get_git_refs()
for y in x:
    print(y)
# output eg:- GitRef(ref="refs/heads/master")

# Select required branch where you want to upload your file.
master_ref = repo.get_git_ref("heads/main")

# Finally, putting everything in a function to make it re-usable

commit_message = 'python commit'
master_ref = repo.get_git_ref('heads/main')
master_sha = master_ref.object.sha
base_tree = repo.get_git_tree(master_sha)

element_list = list()
for i, entry in enumerate(file_names):
    with open(entry) as input_file:
        data = input_file.read()
    element = InputGitTreeElement(file_names[i], '100644', 'blob', data)
    element_list.append(element)

tree = repo.create_git_tree(element_list, base_tree)
parent = repo.get_git_commit(master_sha)
commit = repo.create_git_commit(commit_message, tree, [parent])
master_ref.edit(commit.sha)

Output:

List of Repos on Github under card_chase account:
scrapeOP
Soccer-Betting
Soccer-Predictor


ContentFile(path="README.md")
ContentFile(path="predictions_server.csv")
GitRef(ref="refs/heads/main")
Traceback (most recent call last):
  File "C:\Users\harshad\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_2.py", line 67, in <module>
    with open(entry) as input_file:
FileNotFoundError: [Errno 2] No such file or directory: 'predictions_server.csv'

How can I push predictions_server.csv to the repo?

As you can see, the file exists in repo

edit: Changed code to reflect accurate information

Rander
  • 94
  • 8
  • `for i, entry in enumerate(file_list)` Did you mean to iterate over `file_names` instead of `file_list`? – John Gordon Nov 25 '22 at 04:30
  • Oh my mistake. Now when i iterate over `file_names` I get `with open(entry) as input_file: FileNotFoundError: [Errno 2] No such file or directory: 'file.csv' ` – Rander Nov 25 '22 at 04:36
  • By default, `open()` looks in the current directory. `file.csv` does not exist in the current directory. – John Gordon Nov 25 '22 at 04:38
  • I can see that the [file exists in the repo](https://imgur.com/a/WxGLewD) Probably I am pointing in the wrong direction? – Rander Nov 25 '22 at 04:43
  • `file.csv` is not the same name as the file in the repo `predictions_server.csv` – John Gordon Nov 25 '22 at 04:51
  • Updated actual code. except access token, its the same – Rander Nov 25 '22 at 05:03
  • 1
    `open()` looks for a file on your local computer in the current directory. It doesn't matter if that file exists in the repo... – John Gordon Nov 25 '22 at 05:36

0 Answers0