簡體   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