0

hello guys i try to move all pictures to the folder and i getting an error can you please help me, i have been 25hours looking for the solution on internet Without success, please see the screen

enter image description here


import shutil
import os


class DFO_engine:
    def __init__(self):
        desk_source = open("desk_source.txt", "r")
        direct_files = open("Direction_files.txt", "r")
        link = direct_files.readlines()
        desktop = os.listdir(desk_source.readline())
        pictures_folder = link[0]
        for files in desktop:
            if files.endswith(".jpg"):
                print(files)
                shutil.move(files, pictures_folder)

    desk_source.close()
    direct_files.close()


action = DFO_engine()
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • `shutil.move(desktop + r'\' + files, pictures_folder)` – Olvin Roght Oct 29 '20 at 12:33
  • 1
    You need to prepend the desktop path – jbflow Oct 29 '20 at 12:37
  • @OlvinRoght At the end of the code I see an error, as String https://i.ibb.co/Y7mxYZG/ee.png – soufiane Oct 29 '20 at 12:45
  • @soufiane use `'\\'` – Olvin Roght Oct 29 '20 at 12:54
  • @OlvinRoght shutil.move(desktop + r'\\' + files, pictures_folder) TypeError: can only concatenate list (not "str") to list ?? – soufiane Oct 29 '20 at 13:04
  • @jbflow can you please write code to me !! – soufiane Oct 29 '20 at 13:49
  • change desktop to desk_source.readline() ```shutil.move(desk_source.readline() + r'\\' + files, pictures_folder)``` – jbflow Oct 29 '20 at 13:55
  • For the purposes of best practice you should really be using a ```with``` statement when your opening files. – jbflow Oct 29 '20 at 13:56
  • @jbflow stile i have same problem this is the result https://i.ibb.co/dPpztrr/22222222re.png https://i.ibb.co/Jj0DBsc/ss1111pture.png – soufiane Oct 29 '20 at 14:29
  • just use ```path = os.path.join(desk_source.readline(), files)```, then you should be able to just do: ```shutil.move(path, pictures_folder```At this point can I also ask why you've got your paths held externally in text files, it seems unnecessary – jbflow Oct 29 '20 at 15:11
  • You might still have to deal with the escape characters. Using a raw string literal (r'') would normally do that, but you still can't end with a backslash. This is why I'm asking about the path in the external files as this is adding unnecessary complications. – jbflow Oct 29 '20 at 15:16
  • @jbflow thank you so much, it's working now 100% thank you – soufiane Oct 30 '20 at 08:37

0 Answers0