Jump to content
1 minute ago, Wictorian said:

So I wrote a python script and it pulls data from a .pkl file. But it crashes if there is no data. How can I solve this?

With a try-except - block? Or checking first if the file even exists and then checking if it contains data or not?

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008346
Share on other sites

Link to post
Share on other sites

1 minute ago, Wictorian said:

how do I do that? 

1) I am not familiar with .pkl - files. 2) you haven't shown any of your code. 3) no can do.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008360
Share on other sites

Link to post
Share on other sites

2 minutes ago, WereCatf said:

1) I am not familiar with .pkl - files. 2) you haven't shown any of your code. 3) no can do.

pkl files are not really different from other forms or txt. I dont think you need the code but here you go:

Spoiler

import os
import pickle
action = []
app = []
apps = []
locations = []
syntax = "TASKKILL /F /IM "
def Synchronize(apps, locations):
	fh = open("app locations.pkl", "rb")
	apps = pickle.load(fh)
	locations = pickle.load(fh)
	return(apps,locations)
def registAppsAndLocations(apps, locations):
	fh = open("app locations.pkl", "wb")
	pickle.dump(apps,fh)
	pickle.dump(locations,fh)
def CloseApp(app):
	if "." in app:
		os.system(syntax+app)
	elif app == "os":
		os.system("shutdown /s /t 1")
	else:
		os.system(syntax+(app+".exe"))
def OpenApp(app):
	os.startfile(locations[apps.index(app)])
def Execute(action, app):
	if "c" in action and "l" in action and "o" in action and "s" in action and "e" in action:
		CloseApp(app)
	elif "o" in action and "p" in action and "e" in action and "n" in action:
		OpenApp(app)
	elif "r" in action and "e" in action and "g" in action and "i" in action and "s" in action and "t" in action:
		registAppsAndLocations(apps, locations)
fakeApps, fakeLocations = Synchronize(apps, locations)
apps.append(fakeApps)
locations.append(fakeLocations)
while(True):
	fakeAction = input("Type Action\n")
	fakeApp = input("Type App \n")
	if "r" in fakeAction and "e" in fakeAction and "g" in fakeAction and "i" in fakeAction and "s" in fakeAction and "t" in fakeAction:
		if input("COMMIT? \n") == "COMMIT":
			action.append(fakeAction)
			app.append(fakeApp)
	else:
		action.append(fakeAction)
		app.append(fakeApp)
	Execute(action[len(action)-1], app[len(app)-1])


 

 

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008367
Share on other sites

Link to post
Share on other sites

Just now, Wictorian said:

pkl files are not really different from other forms or txt. I dont think you need the code but here you go:

  Hide contents


import os
import pickle
action = []
app = []
apps = []
locations = []
syntax = "TASKKILL /F /IM "
def Synchronize(apps, locations):
	fh = open("app locations.pkl", "rb")
	apps = pickle.load(fh)
	locations = pickle.load(fh)
	return(apps,locations)
def registAppsAndLocations(apps, locations):
	fh = open("app locations.pkl", "wb")
	pickle.dump(apps,fh)
	pickle.dump(locations,fh)
def CloseApp(app):
	if "." in app:
		os.system(syntax+app)
	elif app == "os":
		os.system("shutdown /s /t 1")
	else:
		os.system(syntax+(app+".exe"))
def OpenApp(app):
	os.startfile(locations[apps.index(app)])
def Execute(action, app):
	if "c" in action and "l" in action and "o" in action and "s" in action and "e" in action:
		CloseApp(app)
	elif "o" in action and "p" in action and "e" in action and "n" in action:
		OpenApp(app)
	elif "r" in action and "e" in action and "g" in action and "i" in action and "s" in action and "t" in action:
		registAppsAndLocations(apps, locations)
fakeApps, fakeLocations = Synchronize(apps, locations)
apps.append(fakeApps)
locations.append(fakeLocations)
while(True):
	fakeAction = input("Type Action\n")
	fakeApp = input("Type App \n")
	if "r" in fakeAction and "e" in fakeAction and "g" in fakeAction and "i" in fakeAction and "s" in fakeAction and "t" in fakeAction:
		if input("COMMIT? \n") == "COMMIT":
			action.append(fakeAction)
			app.append(fakeApp)
	else:
		action.append(fakeAction)
		app.append(fakeApp)
	print(action)
	print(app)
	Execute(action[len(action)-1], app[len(app)-1])


 

 

I'm not that good at python but im trying to figure out

 

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008373
Share on other sites

Link to post
Share on other sites

1 minute ago, Wictorian said:

pkl files are not really different from other forms or txt. I dont think you need the code but here you go:

  Hide contents


import os
import pickle
action = []
app = []
apps = []
locations = []
syntax = "TASKKILL /F /IM "
def Synchronize(apps, locations):
	fh = open("app locations.pkl", "rb")
	apps = pickle.load(fh)
	locations = pickle.load(fh)
	return(apps,locations)
def registAppsAndLocations(apps, locations):
	fh = open("app locations.pkl", "wb")
	pickle.dump(apps,fh)
	pickle.dump(locations,fh)
def CloseApp(app):
	if "." in app:
		os.system(syntax+app)
	elif app == "os":
		os.system("shutdown /s /t 1")
	else:
		os.system(syntax+(app+".exe"))
def OpenApp(app):
	os.startfile(locations[apps.index(app)])
def Execute(action, app):
	if "c" in action and "l" in action and "o" in action and "s" in action and "e" in action:
		CloseApp(app)
	elif "o" in action and "p" in action and "e" in action and "n" in action:
		OpenApp(app)
	elif "r" in action and "e" in action and "g" in action and "i" in action and "s" in action and "t" in action:
		registAppsAndLocations(apps, locations)
fakeApps, fakeLocations = Synchronize(apps, locations)
apps.append(fakeApps)
locations.append(fakeLocations)
while(True):
	fakeAction = input("Type Action\n")
	fakeApp = input("Type App \n")
	if "r" in fakeAction and "e" in fakeAction and "g" in fakeAction and "i" in fakeAction and "s" in fakeAction and "t" in fakeAction:
		if input("COMMIT? \n") == "COMMIT":
			action.append(fakeAction)
			app.append(fakeApp)
	else:
		action.append(fakeAction)
		app.append(fakeApp)
	print(action)
	print(app)
	Execute(action[len(action)-1], app[len(app)-1])


 

 

try some cmd like

b = ("")

If a == b,

             print ("Invalid data in .pkl file")

here a is the content of your file.

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008391
Share on other sites

Link to post
Share on other sites

6 minutes ago, Dujith said:

Does it need to simply check if there is nothing in the file?  If so then a os.path.getsize should work. (it will generate a false if the file size is 0 bytes)

I am pulling data from app locations.pkl that is in the same directory, how do I modify the code?

 

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008429
Share on other sites

Link to post
Share on other sites

12 minutes ago, knockless said:

try some cmd like

b = ("")

If a == b,

             print ("Invalid data in .pkl file")

here a is the content of your file.

it doesnt work because if there is no data I cant even attempt to load it

it says ran out of input

 

 

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008463
Share on other sites

Link to post
Share on other sites

10 minutes ago, Wictorian said:

I am pulling data from app locations.pkl that is in the same directory, how do I modify the code?

 

Well, just before you try to open the file i would do that check. But you havent answered me if the file is empty or just missing at that point?

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008477
Share on other sites

Link to post
Share on other sites

12 minutes ago, Dujith said:

Well, just before you try to open the file i would do that check. But you havent answered me if the file is empty or just missing at that point?

it can also be missing but if it is missing I can add a line to create one. and I am not sure about rb but if you use wb it creates a file if there is no. even if it only checks if there is no data, I would appreciate it.

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008542
Share on other sites

Link to post
Share on other sites

52 minutes ago, Dujith said:

Does it need to simply check if there is nothing in the file?  If so then a os.path.getsize should work. (it will generate a false if the file size is 0 bytes)

 

 

 

os.path.getsize(app locations.pkl):

 also gives syntax error

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14008587
Share on other sites

Link to post
Share on other sites

14 hours ago, elpiop said:

my_path = <path to file>

try:
    if os.path.getsize(my_path) > 0:
        # Non empty file exists
    else:
        # Empty file exists
except OSError as e:
    # File does not exists or is non accessible

 

it gives this error probably I messed up

    apps = pickle.load(fh)
                         ^
TabError: inconsistent use of tabs and spaces in indentation
Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14010613
Share on other sites

Link to post
Share on other sites

6 hours ago, Wictorian said:

it gives this error probably I messed up


    apps = pickle.load(fh)
                         ^
TabError: inconsistent use of tabs and spaces in indentation

You copied the code from the internet without care for indentation. You really should learn more about the tools you're using.

 

Having a look through the code you've posted you'll probably be better off with a helper function or class for file handling. However, that might be overkill so the simplest solution would be something like:

 

def Synchronize(apps, locations):
    if os.path.getsize("app locations.pkl") > 0:
        fh = open("app locations.pkl", "rb")
        apps = pickle.load(fh)
        locations = pickle.load(fh)
        return (apps, locations)
    else:
        # Handle the error here.
        # Your structure is kind of broken atm though. Since fakeApps, and fakeLocations are expecting data at this point
        print("\"app locations.pkl\" is empty. Please fix.")

You'll need to return data from that function (what i mentioned in the comment), or do the checking prior to trying to accessing it (like at the start of main)

def check_files_are_dandy

Or something. 

Link to comment
https://linustechtips.com/topic/1245208-python-question/#findComment-14011244
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×