This question already has answers here:
How to reply to/avoid asking for confirmation from rmdir?
(2 answers)
Cross-platform way of getting temp directory in Python
(5 answers)
Closed 26 days ago.
I wrote a small program that creates 5 folders with random names that each contain a text file. After that I would like to delete them automatically, so I use os.system("rmdir /s {folder_name}"), but I get a confirmation message "are you sure (Y/N)". How can I answer it automatically?
import os
from random import choice
from Alphabet import AlphabetNum
from time import sleep
Folder=[]
for i in range(5):
name=""
for x in range(5):
name+=choice(AlphabetNum())
command=f'mkdir "d:/bin/{name}"'
print(command)
os.system(command)
Folder.append(name)
txt=open("text.txt","x")
txt.write("Text")
txt.close()
os.system(f'move "text.txt" "d:/Bin/{name}"')
sleep(10)
for i in name:
os.system(f'rmdir /s "d:/Bin/{name}"')
sleep(1)
os.system("O")
I tried to reuse os.system by writing os.system("Y"), but it didn't work and can't find any answer.
Related
This question already has answers here:
Where does os.remove go?
(2 answers)
Closed 1 year ago.
I have a more general question about the os.remove() -function.
When I use it to delete a file, does Python delete it entirely from the computer or does Python move it to another folder similar to the trash on mac?
os.remove doesn't send things to Trash. It just deletes them.
Here you go for os.remove docs: https://docs.python.org/3/library/os.html#os.remove
Also i notice similar post on stackoverflow: How to permanently delete a file in python 3 and higher?
os.remove() doesn't send things to Trash. It just deletes them.
You can check it yourself very easily:
import os
file = 'file.txt'
location = "/home/User/Documents"
path = os.path.join(location, file)
os.remove(path)
print("%s has been removed successfully" % file)
This question already has answers here:
Is there an platform independent equivalent of os.startfile()? [duplicate]
(4 answers)
Closed 2 years ago.
I know mac users cannot use os.startfile() but is there any alternative for mac users?
I am just trying to load a file within python but I would like to find a way to do so.
Error:
Module 'os' has no 'startfile' member
You can try os.system with open. For example:
os.system("open Untitled.pdf")
This will open the file Untitled.pdf with the default PDF application ('Preview', in my case).
you should just be able to do:
import subprocess as sp
sp.call(['open', 'application_or_file_name'])
This question already has answers here:
Delete multiple files matching a pattern
(9 answers)
Closed 2 years ago.
I am new to scripting and trying to write a python script to remove a few files.. Here is the path Multiple scripts/script*
Main directory: Multiple scripts
sub directories: script1
script2
script3
In each script in subdirectories, I have file consists of mem. Since I don't know what they start with or their extension now I would like to write a script to search all the subdirectories and delete files that consists of mem.
I tried using the following code but did not work for me
import os
if Multiple scripts/scripts*
os.remove(*/mem*)
else:
print ("file does not exists")
And also please help me with how to write a script to delete files with multiple names (/mem, /name) at a time. Any help would be appreciated... Thank you
If I'm understanding your question correctly, I think you'll want the glob module. Something like
import os
import glob
for fname in glob.iglob("./*/mem*"):
print("Removing "+fname)
os.remove(fname)
Before you run that loop, though, I'd say run it first without the last line, to see what it would do, and make sure that that's what you want.
This question already has answers here:
Open document with default OS application in Python, both in Windows and Mac OS
(17 answers)
Closed 4 years ago.
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
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.
This question already has answers here:
How can I know the path of the running script in Python?
(3 answers)
Closed 8 years ago.
I have the following:
% more a.py
import os
os.system('pwd')
% python a.py
/Users/yl/test/f
% cd ..
% python ./f/a.py
/Users/yl/test
Basically I want the last output to be "/Users/yl/test/f", which is the path where the script is located (not where python was invoked). Have played around but didn't find a good solution. Thanks for any suggestions!
import os
app_dir = os.path.dirname(__file__)
print(app_dir)
import os
print (os.path.dirname(os.path.abspath(__file__)))