-5

I have a robot and it is set up in a way to take commands from the front end of the website to then run a command on the robot. For example

if command == 'CF':
    os.system('aplay /home/pi/sound/wavycf.wav')

I need somthing that will swap two files /images/hud.png and images/special/hud.png to each others directories....I hope that makes sense

pppery
  • 3,731
  • 22
  • 33
  • 46
user3354787
  • 13
  • 1
  • 1
  • 7
  • _I hope that makes sense_ Not really. Try again. Also, show us what you tried so far and post your code. SO is not a coding service. – DYZ Aug 13 '17 at 01:19
  • Possible duplicate of [How to rename a file using Python](https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python); swapping two files is no different than two (or three) rename operations. – pppery Aug 13 '17 at 02:35

1 Answers1

0

Just put one into temporal position, move another and than move first from temp to final place

os.system("move /images/hud.png /images/special/hud.png.tmp")
os.system("move /images/special/hud.png /images/hud.png")
os.system("move /images/special/hud.png.tmp /images/special/hud.png")

or do it everythin using python:

# load file 1 into buffer
data = ""
with open("/images/hud.png", "rb") as f:
    data = f.read()
# copy file 2 into file 1
with open("/images/special/hud.png", "rb") as f1:
    with open("images/hud.png", "wb") as f2:
        f2.write(f1.read())
# save file 1 from buffer
with open("/images/special/hud.png", "wb") as f:
    f.write(data)
# clear memory
del data
Mysak0CZ
  • 904
  • 9
  • 13