0

So I wanted to create code that creates fake steam keys/adobe keys to fool my friends into thinking I can get as many games as I want. So I got everything working as planned, the code generates the steam key and then places it into a .txt file.

However I find it very annoying that I need to keep manually reopening the file after every time I type "y" in the if another == "y": line to access the new key. I was wondering if it is possible to have it open the .txt file for me. I have looked through many websites and can't find anything that actually launches the .txt. Hope someone can help me out here, full code is as follows:

import string
import subprocess
import sys
import random
from random import *
while True:
    def steam():
        while True:
            min_char = 5
            max_char = 5
            allchar = string.ascii_letters + string.digits
            password1 = str("".join(choice(allchar) for x in range(randint(min_char, max_char))))
            password2 = str("".join(choice(allchar) for x in range(randint(min_char, max_char))))
            password3 = str("".join(choice(allchar) for x in range(randint(min_char, max_char))))
            f = open('Steam Keygen.txt','w')
            f.write(password1.upper() + "-" + password2.upper() + "-" + password3.upper())
            f.close()
            steampath = r'C:\Users\mynamewhichIdontwanttoshare\Desktop\Steam Keygen.txt'
            subprocess.Popen(",s ,s" , (steampath))
            another = input("Another?")
            if another == "y":
                print("Ok!")
                steam()
            else:
                sys.exit(0)
    def first(): 
        watchuwant = input("What software do you want a code for?")
        if watchuwant == "steam":
            steam()
        elif watchuwant == "adobe":
            adobe()
        else:
            print("This is not available, sorry.")

    first()

Note:

The adobe() function doesn't work yet so if you want to run it, just test by typing:

-What software do you want a code for?

steam

-Another?

y
CDspace
  • 2,639
  • 18
  • 30
  • 36
pTinosq
  • 73
  • 1
  • 12

1 Answers1

2

If you're trying to get the same result as you would by double-clicking the file, you're looking for os.startfile(). With this, you need to specify a filepath, and when called, this function will 'launch' the file (in this case, your .txt file). The file is opened with whatever application (if any) its extension is associated with.

Usage:

import os
os.startfile('textfile.txt')

This will 'launch' the text file.

Also, as @heather says in the comments, if you only use the filename (and not the filepath), your program will only work if the steam key text file is in the same directory - otherwise, you have to put in the full filepath.

Adi219
  • 4,712
  • 2
  • 20
  • 43
  • [os.startfile][1](steampath) IndexError: list index out of range – pTinosq Jun 08 '18 at 18:40
  • 1
    @T.ps, no, that was due to an error in the link formatting. You want to do `os.startfile('Users\mynamewhichIdontwanttoshare\Desktop\Steam Keygen.txt')`. (Obviously, substitute in your real name.) – Auden Young Jun 08 '18 at 18:41
  • @heather Thanks for the suggested edit ... I was going to approve but Community rejected it ._. – Adi219 Jun 08 '18 at 18:43
  • @T.ps As heather says, you want to use it as a function (using brackets), and pass your filepath as an argument. – Adi219 Jun 08 '18 at 18:44
  • @Adi219 no problem. You might want to note in your "usage" section that just putting 'textfile.txt' will only work if your program is in the same directory as the steam key text file - otherwise, you have to put in the full path (I believe). – Auden Young Jun 08 '18 at 18:44
  • Now i'm getting “Unicode Error ”unicodeescape" codec can't decode bytes – pTinosq Jun 08 '18 at 18:45
  • @heather I've edited that in ... thanks :) – Adi219 Jun 08 '18 at 18:45
  • @T.ps https://stackoverflow.com/a/1347854/7908770 – Adi219 Jun 08 '18 at 18:49