首页 > 编程知识 正文

python复制文件地理数据库,python 复制数据

时间:2023-12-25 12:47:12 阅读:322239 作者:CXMQ

本文目录一览:

python 中如何实现对文件的复制、粘贴

file类中没有提供专门的文件复制函数,因此只能通过使用文件的读写函数来实现文件的复制。这里仅仅给出范例:

src = file("myfile.txt", "w+")

temp = ["hello world! n"]

src.writelines(temp)

src.close()

src = file("myfile.txt", "r+")

des = file("myfile2.txt", "w+")

des.writelines(src.read())

src.close()

des.close()

shutil模块是另一个文件,目录的管理接口,提供了一些用于复制文件,目录的函数。copyfile()函数可以实现文件的拷贝,声明如下:

copyfile(src, des)

文件的剪切可以使用move()函数模拟,声明如下:

move(src,des)

功能:移动一个文件或者目录到指定的位置,并且可以根据参数des重命名移动后的文件。

python 怎么把文件夹下所有文件复制

import

os

import

os.path

rootdir

=

“d:data”

#

指明被遍历的文件夹

for

parent,dirnames,filenames

in

os.walk(rootdir):

#三个参数:分别返回1.父目录

2.所有文件夹名字(不含路径)

3.所有文件名字

for

dirname

in

dirnames:

#输出文件夹信息

print

"parent

is:"

+

parent

print

"dirname

is:"

+

dirname

for

filename

in

filenames:

#输出文件信息

print

"parent

is:"

+

parent

print

"filename

is:"

+

filename

print

"the

full

name

of

the

file

is:"

+

os.path.join(parent,filename)

#输出文件路径信息

说说在 Python 中如何复制、移动、改名以及删除文件或文件夹

要实现复制、移动、改名以及删除文件或文件夹,需要用到 shutil 模块,shutil 是 shell util 的简写形式,表示 shell 工具。

调用 shutil.copy(source, destination) 来实现复制文件或文件夹功能,依据 destination 进行区分:

运行结果:

注意: 指定复制的文件夹必须存在,否则会抛出 FileNotFoundError。

shutil 的 copytree(source, destination) 方法会复制整个文件夹,包括它所包含的所有文件夹和文件。source

指定源文件夹,destination 指定新的文件夹。source 和 destination 入参都是字符串。该函数会返回新文件夹的路径。destination 如果不存在,会自动创建。请看下例:

运行结果:

shutil.move(source, destination) 方法会将路径 source 处的文件移动到路径 destination,并返回新位置的绝对路径的字符串。

如果 destination 指向一个文件夹, source 处的文件将移动到 destination 中, 并保持原来的文件名。

运行结果:

注意:

os 模块中的函数,可以实现删除一个文件或一个空文件夹。而 shutil 更强大,使用它可以删除一个非空文件夹!

注意: 因为是永久删除,所以使用这些函数一定要小心!建议调试程序时, 先注释掉这些删除方法,

然后加上 print(), 把要被删除的文件打印出来,确认后,再执行。

打印出来的文件列表确认无误后,再执行 os.unlink(filename) 执行删除操作。

send2trash 模块会将文件夹或文件发送到计算机的回收站。首先,安装它:

安装成功后,调用 send2trash.send2trash 方法,就可以把文件夹或文件发送到计算机的回收站。请看下例:

建议使用 send2trash.send2trash() 函数来删除文件或文件夹,因为以后还可以从回收站还原。但这样做,不

会释放磁盘空间。如果我们还是希望程序释放磁盘空间, 就要用 os 和 shutil 来删除文件和

文件夹(记得使用之前提出的 print 技巧)。还有一点需要注意, send2trash() 函数只能将文件送到回收站, 但不能从回收站中恢复文件。

python 复制文件

用Python把某一目录下的文件复制到指定目录中,代码如下:

1、首先插入必要的库:

import os 

import os.path 

import shutil 

import time,  datetime

2、实现复制文件代码如下:

def copyFiles(sourceDir,targetDir): 

    if sourceDir.find(".svn")  0: 

        return 

    for file in os.listdir(sourceDir): 

        sourceFile = os.path.join(sourceDir,file) 

        targetFile = os.path.join(targetDir,file) 

        if os.path.isfile(sourceFile): 

            if not os.path.exists(targetDir):  

                os.makedirs(targetDir)  

            if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):  

                    open(targetFile,"wb").write(open(sourceFile,"rb").read()) 

        if os.path.isdir(sourceFile): 

            First_Directory = False 

            copyFiles(sourceFile, targetFile)

3、主函数的实现:

if  __name__ =="__main__": 

    print "Start(S) or Quilt(Q) n" 

    flag = True 

    while (flag): 

        answer = raw_input() 

        if  'Q' == answer: 

            flag = False 

        elif 'S'== answer : 

            formatTime = getCurTime() 

            targetFoldername = "Build " + formatTime + "-01" 

            Target_File_Path += targetFoldername

            copyFiles(Debug_File_Path,Target_File_Path) 

            removeFileInFirstDir(Target_File_Path) 

            coverFiles(Release_File_Path,Target_File_Path) 

            moveFileto(Firebird_File_Path,Target_File_Path) 

            moveFileto(AssistantGui_File_Path,Target_File_Path) 

            writeVersionInfo(Target_File_Path+"\ReadMe.txt") 

            print "all sucess" 

        else: 

            print "not the correct command"

用python提取csv文件内容到数据库

这个脚本可以直接运行,将csv文件放在同级目录即可。

csv第一列需要有列名,如果csv里没有列名,需要在代码中添加列名。

代码运行示例:python insert.py csvname tablename

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。