繁体   English   中英

如何在没有.py文件位于源位置的情况下使程序移动文件

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

我写了这个程序,将视频从我的下载文件夹移动到不同的目标文件夹。

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!')

文件的名称是arrange.py,当arrange.py不在“C:\\ Users \\ Afro \\ Downloads \\ Video”文件夹中时,程序会抛出filenotfound错误。 是否可以让程序在任何文件夹中运行? 谢谢,这是它抛出的错误。

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'

您调用shutil.move错误地指定参考点。 在收集文件的循环中,您应该使用os.path.join('C:\\\\Users\\\\Afro\\\\Downloads\\\\Video', i)并将其放入列表中。 否则它是所有相对路径,因此FileNotFound错误。

您还可以更改工作目录。 这将使脚本的行为就像它在不同的文件夹中一样。 这可能会产生一些意想不到的后果,所以要小心。 细节在这里。

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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