0
import sys
import datetime
import requests
from bs4 import BeautifulSoup

def getYoutubeTags():   
    link = input("Enter link ")
    request = requests.get(link)
    html = BeautifulSoup(request.content,"html.parser")
    tags = html.find_all("meta",property="og:video:tag")

    for tag in tags:
        now = datetime.datetime.now()
        print(tag['content'])
        print(tag['content'], now.strftime("%Y-%m-%d %H:%M"), file=open('output.txt', 'a'), )    
        
getYoutubeTags()

now i want that everytime it creates an outpot.txt file if i am taking another tags from a video it will create a new one

i tried making a loop that detects if there is already an outpot.txt file exists and if so create a new one but i couldn't manage to figure out how to do that i need some guidance ;-;

side note i am only 1 week into python

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • 1
    Use the timestamp to make a unique filename. – Mark Tolonen Dec 08 '22 at 21:10
  • i still have no idea how to make that work let alone search it up as mentioned above i am only 1 week into python @MarkTolonen – Sullivan Park Dec 08 '22 at 21:13
  • Asked and answered on SO already. [How to create a file name with the current date & time in Python?](https://stackoverflow.com/q/10607688/235698) – Mark Tolonen Dec 08 '22 at 21:18
  • i was not asking on how to create / open a file with python .... – Sullivan Park Dec 08 '22 at 21:42
  • Do you want to overwrite the file each time? Your question is unclear. I was suggesting writing a new file each time with a unique filename, hence the link. But if I misunderstood, ask a clear question. – Mark Tolonen Dec 08 '22 at 21:44
  • ohh i am sorry yeah i was in the wrong here i used "w" at the end of the outpot text line however once the outpot text is created it's only giving me ONE line of tags instead of the whole tags weird thing is i get the entire tags shown at the terminal but not at the outpot file itself – Sullivan Park Dec 08 '22 at 21:53

3 Answers3

0

You could create a timestamp to the name of the file the same way you created timestamp for the output:

def getYoutubeTags():   
    link = input("Enter link ")
    request = requests.get(link)
    html = BeautifulSoup(request.content,"html.parser")
    tags = html.find_all("meta",property="og:video:tag")
    

    for tag in tags:
        now = datetime.datetime.now()
        print(tag['content'])
        print(tag['content'], now.strftime("%Y-%m-%d %H:%M"), file=open('output' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '.txt', 'a'), )    

You also have the function time.time() (using import time) which returns the time as a floating point number expressed in seconds since the epoch. You can then transfer it to int (or not) and that way you make sure you won't have any duplicates.

import time
def getYoutubeTags():   
    link = input("Enter link ")
    request = requests.get(link)
    html = BeautifulSoup(request.content,"html.parser")
    tags = html.find_all("meta",property="og:video:tag")
    

    for tag in tags:
        now = datetime.datetime.now()
        print(tag['content'])
        print(tag['content'], now.strftime("%Y-%m-%d %H:%M"), file=open('output' + str(time.time()) + '.txt', 'a'), ) 

Note:

If the purpose of the new file is that the program won't override the file each time you run it, you can just use the 'a' mode of opening a file.

You can read more about modes of opening files in python in here.

nokla
  • 451
  • 1
  • 8
  • the first segment gives me an OS Error and the second one either gives me 10 files cause it's 0.000000001ms writing the files – Sullivan Park Dec 08 '22 at 21:33
  • Weird, the first is working to me just fine, what is the error exactly? – nokla Dec 08 '22 at 22:15
  • You should use the `int()` or `math.floor()` functions on the second one to make the time change only by seconds. – nokla Dec 08 '22 at 22:16
0

If you want to rewrite the existing file, you can write the very first entry without 'a' option in open(), which stands for "append" and adds new lines to the end of the file. Without this flag, the file will be overwritten.

0

first thanks everyone for the help and the tips

generally what i did is basically

made the user make a file himself

namefile = input("File name ")

then in the print tab

print(tag['content'], now.strftime("%Y-%m-%d %H:%M", ), file=open(namefile +'.txt','a'))

this is the finished code

import sys
import datetime
import requests
import time
import os
from bs4 import BeautifulSoup


def getYoutubeTags():   
    namefile = input("File name ")
    link = input("Enter link ")
    request = requests.get(link)
    html = BeautifulSoup(request.content,"html.parser")
    tags = html.find_all("meta",property="og:video:tag")
    

    for tag in tags:
        now = datetime.datetime.now()
        print(tag['content'])
        print(tag['content'], now.strftime("%Y-%m-%d %H:%M", ), file=open(namefile +'.txt','a'))
        

        
getYoutubeTags()