matrixratrix
Posts: 17
Joined: Sat Sep 15, 2018 10:39 am

Move ERROR

Mon May 20, 2019 6:15 pm

Hello,

I have this code
:

Code: Select all

import time
import shutil
from random import randint


RegRasp="/home/pi/Desktop/new"

def MoveToServer():
    
    data=(time.strftime('%d-%m-%Y'))
    hora=(time.strftime('%H-%M-%S'))
    Random1 = randint(1, 10000) + randint(10000, 90000)
    fileNome= str(data) + "_" + str(hora) + "_" + str(Random1) + ".txt"
    try:  
        shutil.move( RegRasp , "/home/pi/Documentss/"+ fileNome )    
    except shutil.Error:
        print ("error")
        return
		
    print ("move ok")

	
	
MoveToServer()


I try make a script thats move file to another folder, this work ok whend the destination folder exist, and if there is no error.


But I want that, if any reazon the file dont move this cause the exception and my script keep runing with no problem.


This try:/except simply dont work, what is rong?


Best regards
[moderated]

Andyroo
Posts: 2834
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Move ERROR

Mon May 20, 2019 6:45 pm

Hello,

I have this code
:

Code: Select all

import time
import shutil
from random import randint


RegRasp="/home/pi/Desktop/new"

def MoveToServer():
    
    data=(time.strftime('%d-%m-%Y'))
    hora=(time.strftime('%H-%M-%S'))
    Random1 = randint(1, 10000) + randint(10000, 90000)
    fileNome= str(data) + "_" + str(hora) + "_" + str(Random1) + ".txt"
    try:  
        shutil.move( RegRasp , "/home/pi/Documentss/"+ fileNome )    
    except shutil.Error:
        print ("error")
        return
		
    print ("move ok")

	
	
MoveToServer()


I try make a script thats move file to another folder, this work ok whend the destination folder exist, and if there is no error.


But I want that, if any reazon the file dont move this cause the exception and my script keep runing with no problem.


This try:/except simply dont work, what is rong?


Best regards
Need Pi spray - these things are breeding in my house...

Andyroo
Posts: 2834
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Move ERROR

Mon May 20, 2019 6:48 pm

I’ve added code tags </> button to show the indent.

If you could keeps running then the try/except has worked fine.

The try function does not stop if there is an error - it just passes control to the except part of the function.

What exactly are you expecting?
Need Pi spray - these things are breeding in my house...

matrixratrix
Posts: 17
Joined: Sat Sep 15, 2018 10:39 am

Re: Move ERROR

Tue May 21, 2019 7:50 am

The try function does not stop if there is an error - it just passes control to the except part of the function.

What exactly are you expecting?
I expect exactly this, that the error does not stop the script and follow its path.


I have this inside a loop, and whenever the error occurs the loop is broken, maybe this try: is not well done?


Thanks

Ernst
Posts: 1090
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: Move ERROR

Tue May 21, 2019 8:46 am

I don't know much about python but I looked around a bit, tried a few things, learned a little bit, and I got it working for the case that the target directory does not exist.

Code: Select all

[email protected]:~ $ ls Desktop/ -l
total 4
-rw-r--r-- 1 pi pi 4 May 21 10:10 new
[email protected]:~ $ ls Desktop/documentss -l
ls: cannot access 'Desktop/documentss': No such file or directory
[email protected]:~ $ python test.py
# =====> MoveToServer
failed IO
[Errno 2] No such file or directory: '/home/pi/Documentss/21-05-2019_10-43-44_78013.txt'
# <===== MoveToServer
[email protected]:~ $
This is what I put together:

Code: Select all

import time
import shutil
from random import randint


RegRasp="/home/pi/Desktop/new"

def MoveToServer():

    data=(time.strftime('%d-%m-%Y'))
    hora=(time.strftime('%H-%M-%S'))
    Random1 = randint(1, 10000) + randint(10000, 90000)
    fileNome= str(data) + "_" + str(hora) + "_" + str(Random1) + ".txt"
    try:
        shutil.move( RegRasp , "/home/pi/Documentss/"+ fileNome )
        print ("move ok")
    except OSError as err:
        print ("failed OS")
        print(err)
    except IOError as err:
        print ("failed IO")
        print(err)
    except:
        print ("failed")

print("# =====> MoveToServer")
MoveToServer()
print("# <===== MoveToServer")

The road to insanity is paved with static ip addresses

matrixratrix
Posts: 17
Joined: Sat Sep 15, 2018 10:39 am

Re: Move ERROR

Tue May 21, 2019 9:50 am

Thank you, work perfect.


Best Regards

matrixratrix
Posts: 17
Joined: Sat Sep 15, 2018 10:39 am

Re: Move ERROR

Tue May 21, 2019 12:31 pm

Hello,

Just one more question:

I need chek if a unit is mounted or not, this code work:

Code: Select all

        if os.path.ismount("/home/pi/registos"):
                print ("Mount is ok")

The only problem is , if the init is not monted this code return false that is ok to, but take much time to return the false, there
is any way to reduce the time return case is false?


Thanks

Return to “Python”