簡體   English   中英

Python:FileNotFoundError:[WinError 3]系統找不到指定的路徑:”

[英]Python: FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

我正在用Python創建一個程序來自動將閃存驅動器備份到其中。 第一次備份,效果很好,但是當它再次重復備份過程時,我收到此錯誤消息。

File "D:\Programming\_Languages\Python\Automatic_Backup\Automatic_Backup.py", line 51, in <module>
    shutil.copytree(src, dst)
  File "C:\Users\carso\AppData\Local\Programs\Python\Python35-32\lib\shutil.py", line 303, in copytree
    names = os.listdir(src)
FileNotFoundError: [WinError 3] The system cannot find the path specified: ''

path_dst.txt的內容為“ C:// Users // carso // Dropbox // Backup”(無引號)。 path_src.txt的內容為“ D:// Programming”(再次,無引號)。

我一直在嘗試尋找解決方案,但是我不知道如何解決。 這是程序的代碼,希望對您有所幫助。

import shutil
import os
import time

with open('program_started.txt', 'r+') as program_started_file:
    # If program was previously started, runs instructions. If not, continues as normal
    program_started = program_started_file.read()
    if program_started == 'False':
        # Gets src path file and dst path file, write only
        src_file = open('path_src.txt', 'w')
        dst_file = open('path_dst.txt', 'w')
        # Gets src and dst paths
        src_path = input('Enter source path: ') 
        dst_path = input('Enter destination path: ')
        # Writes src and dst paths to txt file
        src_file.write(src_path)
        dst_file.write(dst_path)
        # Moves to beginning of document
        program_started_file.seek(0)
        # Writes 'True' in front of prevous 'False'
        program_started_file.write('True')
        # Removes 'False'
        program_started_file.truncate()
        # Displays 'Completed' message
        print("Completed getting source and destination paths")
    elif program_started == 'True':
        # Gets src path file and dst path file, read only
        src_file = open('path_src.txt', 'r')
        dst_file = open('path_dst.txt', 'r')
        # Checks if flash drive is plugged in
        while True:
            if os.system('cd D:') == 0:
                # Stores src path and dst path in string
                src = src_file.read()
                dst = dst_file.read()
                # If a 2nd backup has been made, removes first and renames 2nd
                if os.path.isdir(dst + "_2") == True:
                    os.rmdir(dst)
                    os.rename(dst + "_2", dst)
                    dst = dst + "_2"
                 # If only a 1st backup was made, creates a 2nd
                elif os.path.isdir(dst) == True:
                    dst = dst + "_2"
                # Copies data
                print('Backing up...', end='')
                shutil.copytree(src, dst)
                print('Completed')
                # Sleeps for 20 minutes
                for x in range(1,12):
                    print("Second: ", x)
                    time.sleep(1)
            else:
                # If no flash drive is detected, tries again in 5 minutes. 
                time.sleep(600)
    else:
        # Error message if program_started.txt != true or false
        print("Error: program_started.txt must only contain either 'True' or 'False'.")

您已經在第一次備份期間讀取並耗盡了文件,因此將為以后的讀取返回一個空字符串=無效路徑= FileNotFoundError

您必須使用seek()回到開頭。 地點:

src_file.seek(0)
dst_file.seek(0)

while True:

如果某些文件是只讀的,則可能會阻止rmtree()工作。 定義此功能:

def remove_readonly(func, path, _):
    "Clear the readonly bit and reattempt the removal"
    os.chmod(path, stat.S_IWRITE)
    func(path)

然后像這樣調用rmtree()

shutil.rmtree(dst, onerror=remove_readonly)

您必須提供絕對路徑,而不是相對路徑。

我寫了一個簡單的代碼來幫助您。

import os
import shutil

source1 = os.listdir("C:/Users/ved/Documents/source/")
destination = "C:/Users/ved/Documents/dest"


for file in source1:
   fname=file
   x=("C:/Users/Eccella/Documents/source/"+fname)  #for giving absolute path
   #print(x)
   shutil.move(x, destination    #for moving from absolute path to destination folder

只需從路徑中刪除空格,例如:

path = your_path.strip()

這樣就可以了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM