python - "No such file" error when running an autoscript -


i working on project suppose open during start of raspberry pi. can open several other scripts on start up, script trying open during start not open.

note: code works when run through idle, not work when test through lx terminal. when test script under auto run script gives me error.
another note: image files in same folder script.

here code:

from tkinter import * pil import image, imagetk random import randint import rpi.gpio gpio  root = tk() root.overrideredirect(true) root.geometry("1920x1080+0+0")  image=image.open('bikebackground.png') background_image = imagetk.photoimage(image) background_label = label(root, image=background_image) background_label.place(x=0, y=0, relwidth=1, relheight=1)  speed = 4  class biker(label): def __init__(self, master, filename):     im = image.open(filename)     seq =  []     try:         while 1:             seq.append(im.copy())             im.seek(len(seq)) # skip next frame     except eoferror:         pass # we're done      try:         self.delay = im.info['duration']/speed     except keyerror:         self.delay = 100      first = seq[0].convert('rgba')     self.frames = [imagetk.photoimage(first)]      label.__init__(self, master, image=self.frames[0])      temp = seq[0]     image in seq[1:]:         temp.paste(image)         frame = temp.convert('rgba')         self.frames.append(imagetk.photoimage(frame))      self.idx = 0      self.cancel = self.after(self.delay, self.play)  def play(self):     self.config(image=self.frames[self.idx])     self.idx += 1     if self.idx == len(self.frames):         self.idx = 0     self.cancel = self.after(self.delay, self.play)  anim = biker(root, 'racer2.gif') anim.place(relx=0, rely=0, relwidth=1, relheight=1)  w = label(root, text= 'you generating\n%d.%d watts\nof power' %(randint(10,15),   randint(0,99)), font=("helvetica", 50)) w.place(relx=.6, rely=.5, relwidth=.4, relheight=.25)  wh = label(root, text= 'you have generated\n%d.%d kilojoules\nof energy' %(randint(0,2),        randint(0,99)), font=("helvetica", 50)) wh.place(relx=.6, rely=.75, relwidth=.4, relheight=.25) root.mainloop() 

in lx terminal, while testing code here error:

pi@raspberrypi ~/bin $ /home/pi/bin/script_auto_run doing autorun script... pi@raspberrypi ~/bin $ traceback (most recent call last):  file "/home/pi/new/display.py", line 10, in <module>    image=image.open('bikebackground.png')   file "/usr/local/lib/python2.7/dist-packages/pil/image.py", line 2093, in open    fp = builtins.open(fp, "rb") ioerror: [errno 2] no such file or directory: 'bikebackground.png' 

it seems script not run same folder, not find pictures in working directory. should append absolute path of folder image path:

import os.path  # folder on current file (/home/pi/new case) script_dir = os.path.basename(__file__)  # , use os.path.join concatenate paths image = image.open(os.path.join(script_dir, 'bikebackground.png')) 

.. or explicitly move in folder of script before launching script.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -