Delete a runing file? (Python) - python

I'm trying to delete my .exe file after runing... (written in PY)
Is there any way?
#I have tried CMD and admin.
FILEPATH = sys.argv[0]pe here
os.remove(FILEPATH)

Related

Trouble finding file directory using Chrome OS

I'm trying to read in a .txt file on chromebook to manipulate the data that is there. My code to source the file is:
def readInFile():
arr_intValues = []
myFile = open("#MY FILE HERE", "r")
#you need to change the directory
for myLine in myFile:
arr_intValues.append(int(myLine))
return arr_intValues
myNewList = readInFile()
print(myNewList)
Trouble is, i cannot find out where the source of the file is for this code. If i drop the file into a chrome tab, it reads:
file:///media/fuse/crostini_9549350604ce9beeb7d5a9a66d5bf09733144d34_termina_penguin/RandomNum.txt
Meanwhile the file location that "Get info" returns is:
My files/Linux files/RandomNum.txt
Both of these options fail if I attempt to open it and print it in my code.
How do I find the correct file directory?
I attempted to find the directory of a .txt file on using Chrome OS and have not been successful.
If you're running the program on Linux, you'll need to specify the Linux path.

Compiled exe needs to be run as admin but uncompiled py file doesn't?

I recently compiled a python project I have been working on for some time to an exe, only to find you'd need to run it as an administrator for it to work, despite it not requiring any elevation while it was simply a .py file. It returns an internal error "cannot create temporary directory!" if you don't run it with elevation. I presume this has to do something with the program reading/writing to the users appdata/locallow folder (it stores some of it's files in there). Is there a simple fix to this? I'd much prefer it if the apps files could remain in one of the appdata folders whether it be local, locallow or roaming.
This can be reproduced by following these steps:
create a main.py file with the following code inside:
import os, json
LOG_DIR = os.path.expanduser('~\AppData\Locallow\AppName\logs')
LOG_PATH = os.path.expanduser('~\AppData\Locallow\AppName\logs\log.txt')
PREFS_DIR = os.path.expanduser('~\AppData\Locallow\AppName\prefs')
PREFS_PATH = os.path.expanduser('~\AppData\Locallow\AppName\prefs\prefs.json')
user_prefs = None
def log_init():
if os.path.isfile(LOG_PATH):
with open(LOG_PATH, 'a') as f:
f.close()
else:
if not os.path.isdir(LOG_DIR):
os.mkdir(LOG_DIR)
with open(LOG_PATH, 'w') as f:
f.close()
def prefs_init():
global user_prefs
if os.path.isfile(PREFS_PATH):
user_prefs = json.load(open(PREFS_PATH, 'r'))
else:
if not os.path.isdir(PREFS_DIR):
os.mkdir(PREFS_DIR)
with open(PREFS_PATH, 'w') as f:
f.write('{"welc_msg": "DirNav - The all in one file browser and terminal!", "file_color": {}}')
f.close()
user_prefs = json.load(open(PREFS_PATH, 'r'))
log_init()
prefs_init()
Make the following folder: C:/Users/YOUR_USER/AppData/Locallow/AppName
Running main.py will generate 'prefs' and 'logs' with respective files inside.
Delete 'prefs' and 'logs'
Compile main.py using this command: pyinstaller -F main.py
Running the new .exe that is in the 'dist' folder from cmd will result in an 'INTERNAL ERROR: cannot create temporary directory!' message.
Run the .exe again but this time run as an administrator and it will work perfectly fine
The main premise here is why does the executable need to be run as an admin yet the .py file doesn't? And how do I make it so the executable doesn't need to be run as an admin?

why i can not convert .py that conatain pytesseract.pytesseract to .exe file

I need to extract arabic text from image so I use pytesseract.pytesseract and it works in PyCharm editor but when I convert .py file to .exe file it not work.
def do():
try:
global scr
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # th path of tesseract
img_entered = Image.open(nu.get())
text_ = pytesseract.image_to_string(img_entered, lang='ara')
scr.insert(index=tk.END,chars=text_)
nu.set("")
except:
messagebox.showerror("error","لا يمكن استخراج نص من هذا الملف")
When i run .exe file it gives me the correct GUI but when it needs to convert image to text it always do the except from the do() function.
My questions:
how can i solve this ?
is there are any another way to convert image to text ?
before running the converted .exe file comes from .py file ,
the tesseract.exe file must be installed in pc in the folder that you define in .py file here in my case is C:\Program Files\Tesseract-OCR\tesseract.exe

How to open a .txt file in Flask? [duplicate]

This question already has an answer here:
Refering to a directory in a Flask app doesn't work unless the path is absolute
(1 answer)
Closed 5 years ago.
I'm trying to build a website using the Flask framework for Python.
I'm on a Linux Ubuntu server, with Apache2.
On my website, whenever someone enters the URL "/Elv_1.html", I want to open a .txt file, get some values and create a graph using pygal. Here is my code:
#app.route('/river_1.html')
def riv_1():
try:
document = open('temp.txt','r')
temp_list = []
for n in document:
n = n.rstrip('\n')
n = int(n)
temp_list.append(n)
document.close()
graf = pygal.Line(title=u'Tempt last 24h')
graf.x_labels = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)
graf.add('Temp', temp_list)
graf = graf.render_data_uri()
return render_template('river_1.html', graf=graf)
except Exception, e:
return str(e)
if __name__ == '__main__':
app.run(debug=True)
The file 'temp.txt' is located in the same directory as the __init__.py file. __init__.py is the Flask app that the code comes from.
When I do this on my computer using localhost to run the server, it works just fine. However, when I upload this to my Linux server and try to enter that specific URL, it shows the following error:
[Error 2] No such file or directory: 'temp.txt'
Any suggestions as to why it doesn't appear to find the file?
Try using the os module when specifying the path to your file. I am asuming you are using a windows pc when runing on localhost?
import os
document_path = os.getcwd()+'temp.txt'
document = open(documnet_path, 'r')
Make sure you are running the server from it's directory. So if you have this structure as shown below, you can't simply open terminal and type server/__init__.py, because you are in your home directory (/home/username/). You need to cd to server and there run ./__init__.py
/home/
username/
server/
__init__.py
temp.txt
Or, if you want to run it from somewhere else, run open the file from os.path.abspath(os.path.dirname(__file__)) + '/temp.txt') (Tested with python 3.5.2)
See python docs for os.path.

file line writing closes off script

I'm looking to make a file that when you enter a directory, it creates a folder inside that directory. I'm using a batch file to create the folder, so I'm making it when you enter the directory, it will write that directory to the batch file, then run it. However I'm having some errors. Here's my code
directory = str(input())
text = 'cd ' + directory
lines = open('new.bat', 'r').readlines()
lines[2] = text
out = open('new.bat', 'w')
out.writelines(lines)
out.close()
call('new.bat')
out.close() exits my python script before it can have a chance to call 'new.bat', however if I move the out.close() to after it calls new.bat, it gives me an error that the file is currently being used. How do I fix this?

Categories

Resources