如果目录不存在,使用Pythons shutil创建一个目录?

2024-10-02 10:26:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我的网络上有一个程序的副本。如果其他计算机已经指定了目录,则此功能正常。在

某些计算机可能还没有指定的目录。在这种情况下,如果目录不存在,如何创建它,然后将文件复制到新目录中。会shutil.copytree公司()工作?在

这是我目前计划的一部分

import fileinput
import shutil

def copyerror():
    print (" //////////////////////////////////////////")
    print (" ")
    print (" Cannot connect to " + servername + ".")
    print (" ")
    print (" //////////////////////////////////////////")   

def copyfiles(servername):
    source = os.listdir("C:/Users/jm/Desktop/PythonUpdate/") 
    destination = '//' + servername + '/c$/test/' 
    for files in source:
        if files.endswith(".config"):
            try:
                shutil.copy(files,destination)
            except:
                copyerror()


array = []
with open("C:/Users/jm/Desktop/PythonUpdate/serverlist.txt", "r") as f:
    for servername in f:

        copyfiles(servername.strip())

因此,如果/test目录不存在,我想继续创建一个。在

更新 另外,有没有一种方法可以停止将文件复制到新计算机上?如果目录不存在的话。在


Tags: 文件import目录sourcedef计算机filesusers

热门问题