简体   繁体   中英

How do I make my program move files without the .py file being in the source location

I wrote this program to move videos from my download folder to different destination folders.

import os
import shutil
import sys
folder = []
highlight = ['highlights','goals','skills']
comedy_word = ['comedy', 'acapella','seyilaw','basketmouth','basket mouth','bovi','gordons','buchi','rhythm unplugged','elenu','seyi law','akpororo','emmaohmygod','klint','shakara','funnybone','igodye','i go die','i go dye','igodye','whalemouth','whale mouth','daniel']
series_word = ['lost', 'thrones', 'vampire', 'originals', 'ship', '']
grub_word = ['programming', 'python', 'linux','exploit','hack']


for i in os.listdir('C:\\Users\\Afro\\Downloads\\Video'):
    folder.append(i.lower())


def tv_series(series):
    for serie in series:
        if 'ship' in serie:
            shutil.move(serie, 'C:\\Users\\Afro\\Desktop\\Movies\\Series\\The Last Ship\\The Last Ship 3\\' )
            print(serie[:-3] +': Moved!')

        elif 'lost' in serie:
            shutil.move(serie, 'C:\\Users\\Afro\\Desktop\\Movies\\Series\\Lost\\s2\\' )
            print(serie[:-3] +': Moved!')

The file's name is arrange.py and the program throws a filenotfound error when arrange.py is not in the "C:\\Users\\Afro\\Downloads\\Video" folder. Is it possible to make the program run in any folder? Thanks Here's the error it throws.

Traceback (most recent call last):
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 538, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'lost - s02e08 (o2tvseries.com).mp4' -> 'C:\\Users\\Afro\\Desktop\\Movies\\Series\\Lost\\s2\\lost - s02e08 (o2tvseries.com).mp4'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\Afro\Desktop\Arrange.py", line 77, in <module>
start(folder)
File "C:\Users\Afro\Desktop\Arrange.py", line 65, in start
tv_series(folder)
File "C:\Users\Afro\Desktop\Arrange.py", line 22, in tv_series
shutil.move(serie, 'C:\\Users\\Afro\\Desktop\\Movies\\Series\\Lost\\s2\\' )
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 552, in move
copy_function(src, real_dst)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 251, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\Afro\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 114, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'lost - s02e08 (o2tvseries.com).mp4'

You call shutil.move specifying the reference point incorrectly. In your loop that gathers the files, you should use os.path.join('C:\\\\Users\\\\Afro\\\\Downloads\\\\Video', i) and put that in your list instead. Otherwise it's all relative paths, hence the FileNotFound error.

You could also change the working directory. This would make the script behave as if it was in a different folder. This can have some unintended consequences, so be careful. Details here.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM