I am new to python and am trying to make a YT username to channel ID converter, It takes a txt input of channels/usernames and I want it to send the results from the API to the output.txt. But it reads from the whole output instead of just the newest/last line.
I tried using readlines(-1) but it seems to just not do anything at all.
from googleapiclient.discovery import build
import time
Channellist = open('input.txt','r').readline()
output = open('output.txt','r').read()
api_key = 'KEY'
youtube = build('youtube', 'v3', developerKey=api_key)
request = youtube.channels().list(
part='snippet',
id=(Channellist))
response = request.execute()
with open('output.txt', 'a', encoding="utf-8" ) as f:
f.write(str(response) + '\n')
if output.__contains__("snippet") == True:
print('Valid Channel ID/Username Found')
time.sleep(1)
print(response)
with open('output.txt','r') as fp1:
x = len(fp1.readlines())
time.sleep(0.5)
print('Total in Output:', x)
else:
print('Invalid Channel ID/Username')
with open('output.txt','r+') as fp:
lines = fp.readlines()
fp.seek(0)
fp.truncate()
fp.writelines(lines[:-1])
with open('input.txt', 'r') as fin:
data = fin.read().splitlines(True)
with open('input.txt','w') as fout:
fout.writelines(data[1:])
exit()
with open('input.txt', 'r') as fin:
data = fin.read().splitlines(True)
with open('input.txt','w') as fout:
fout.writelines(data[1:])
exit()
I honestly just downloaded python 2 days ago and haven't done any tutorials. So I really don't know what's going wrong.