要使用GitHub API列出一个目录下的所有文件名,可以按照以下步骤进行操作:
import requests
def list_files_in_directory(repo_owner, repo_name, directory_path, access_token):
headers = {
"Authorization": f"Bearer {access_token}"
}
url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/contents/{directory_path}"
response = requests.get(url, headers=headers)
if response.status_code == 200:
files = [file["name"] for file in response.json()]
return files
else:
return None
# 使用示例
repo_owner = "your_github_username"
repo_name = "your_repository_name"
directory_path = "path_to_directory"
access_token = "your_personal_access_token"
files = list_files_in_directory(repo_owner, repo_name, directory_path, access_token)
if files:
for file in files:
print(file)
else:
print("Failed to retrieve file list.")
在上述代码中,你需要将repo_owner
替换为你的GitHub用户名,repo_name
替换为你的仓库名称,directory_path
替换为你想要列出文件的目录路径,access_token
替换为你的PAT。
请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。此外,GitHub API还提供了其他功能,如获取文件内容、创建文件等,你可以根据需要进一步探索。
推荐的腾讯云相关产品:腾讯云对象存储(COS)。腾讯云对象存储(COS)是一种安全、持久、高扩展性的云端存储服务,适用于存储大量非结构化数据,如图片、音视频、备份和存档数据等。你可以通过以下链接了解更多信息:腾讯云对象存储(COS)。
领取专属 10元无门槛券
手把手带您无忧上云