0

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.

dan
  • 1
  • 1
  • Can you explain in English what exactly each part of this code is supposed to be doing? It's pretty clear that there's some confusion about what specific statements/functions do, but it's not possible to write a corrected version without knowing what exactly you were trying to do. E.g. when you wrote `output.__contains__('snippet')`, is that something you're trying to do on each line of the file, or on the last line of the file, or on the file as a whole? And which file? – Samwise Jul 05 '23 at 00:25
  • I tried to get it to only check if it contains 'snippet' on the last line of the output.txt file. Output is above and is just where the API response goes. I can barely understand my own code haha – dan Jul 05 '23 at 00:46

1 Answers1

0

If I understand you, you just want to read the last line of a text file. here are two ways this could be done:

Splitting the file into lines, and then taking the last line:

with open('filename.txt') as f:
    lines = f.read().split("\n")
    last_line = lines[-1]  # NOTE: If your file ends in a newline, last_line will be empty
    # Do whatever you want with the last line:
    print(last_line)

Note that if the file ends in a newline last_line will be an empty string. If you need to handle files with newlines change the line:

lines = f.read().split("\n")

to:

lines = f.read().strip().split("\n")

Reading each line until the end:

As suggested here:

with open('filename.txt') as f:
    for line in f:
        pass
    last_line = line