簡體   English   中英

shutil.copy只工作一次

[英]shutil.copy only works once

作為Python初學者,我在移動文件時遇到了實際問題。 下面是我終於完成的腳本(!),可以簡單地將選擇文件從選擇的目錄移動到新文件夾。 出於某種我無法理解的原因,它只能工作一次,然后創建的目標文件夾確實很奇怪。 一方面,它創建了一個“目錄”,該目錄是一個具有正確名稱的未知應用程序;另一方面,它使用看似隨機的文件來生成內容來創建文本文件-再次正確地命名了它創建的文件。

這是相關的腳本:

#!/usr/bin/python

import os, shutil

def file_input(file_name):                
    newlist = []                                                #create new list
    for names in os.listdir(file_name):                         #loops through directory
        if names.endswith(".txt") or names.endswith(".doc"):    #returns only extensions required    
            full_file_name = os.path.join(file_name, names)     #creates full file path name - required for further file modification
            newlist.append(full_file_name)                      #adds item to list
            dst = os.path.join(file_name + "/target_files")
            full_file_name = os.path.join(file_name, names)
            if (os.path.isfile(full_file_name)):
                print "Success!"
                shutil.copy(full_file_name, dst)

def find_file():
    file_name = raw_input("\nPlease carefully input full directory pathway.\nUse capitalisation as necessary.\nFile path: ")
    file_name = "/root/my-documents"                            #permanent input for testing!
    return file_input(file_name)
    '''try:
        os.path.exists(file_name)
        file_input(file_name)
    except (IOError, OSError):
        print "-" * 15
        print "No file found.\nPlease try again."
        print "-" * 15
        return find_file()'''

find_file()

有人可以告訴我為什么刪除創建的文件夾並嘗試再次運行該腳本時該腳本無法復制嗎?

我知道這有點混亂,但這將是較大腳本的一部分,而我仍處於初稿階段!

非常感謝

這有效:

import os, shutil

def file_input(file_name):                
    newlist = []                                                #create new list
    for names in os.listdir(file_name):                         #loops through directory
        if names.endswith(".txt") or names.endswith(".doc"):    #returns only extensions required    
            full_file_name = os.path.join(file_name, names)     #creates full file path name - required for further file modification
            newlist.append(full_file_name)                      #adds item to list
            dst = os.path.join(file_name + "/target_files")

            if not os.path.exists(dst):
                os.makedirs(dst)

            full_file_name = os.path.join(file_name, names)
            if (os.path.exists(full_file_name)):
                print "Success!"
                shutil.copy(full_file_name, dst)

def find_file():
    file_name = raw_input("\nPlease carefully input full directory pathway.\nUse capitalisation as necessary.\nFile path: ")
    file_name = "/home/praveen/programming/trash/documents"                            #permanent input for testing!
    return file_input(file_name)

find_file()

您需要檢查您的復制目標目錄是否實際存在,如果沒有創建的話。 shutil.copy然后將文件復制到該目錄

暫無
暫無

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

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