rustpython,shutilpython_shutil

 2023-11-19 阅读 32 评论 0

摘要:#### python實現拷貝指定文件到指定目錄方法1:1.path使用絕對路徑2.path使用相對路徑,需要注意:相對路徑前會補上當前執行腳本的絕對路徑3.文件名不要有中文rustpython。~~~import osimport shutilshutil.copyfile(oldpath,newpath)# path是要拷貝的文件

#### python實現拷貝指定文件到指定目錄

方法1:

1.path使用絕對路徑

2.path使用相對路徑,需要注意:相對路徑前會補上當前執行腳本的絕對路徑

3.文件名不要有中文

rustpython。~~~

import os

import shutil

shutil.copyfile(oldpath,newpath)# path是要拷貝的文件的路徑

~~~

方法2:

python shutil模塊?1.編寫腳本

~~~

import os,sys,shutil

### copies a list of files from source. handles duplicates.

def rename(file_name, dst, num=1):

#splits file name to add number distinction

chrpython中,(file_prefix, exstension) = os.path.splitext(file_name)

renamed = "%s(%d)%s" % (file_prefix,num,exstension)

#checks if renamed file exists. Renames file if it does exist.

if os.path.exists(dst + renamed):

return rename(file_name, dst, num + 1)

else:

Python shell、return renamed

def copy_files(src,dst,file_list):

for files in file_list:

src_file_path = src + files

dst_file_path = dst + files

if os.path.exists(dst_file_path):

python tools、new_file_name = rename(files, dst)

dst_file_path = dst + new_file_name

print "Copying: " + dst_file_path

try:

# 復制操作主要就是這句

shutil.copyfile(src_file_path,dst_file_path)

python 打包,except IOError:

print src_file_path + " does not exist"

raw_input("Please, press enter to continue.")

def read_file(file_name):

f = open(file_name)

#reads each line of file (f), strips out extra whitespace and

python gui,#returns list with each line of the file being an element of the list

content = [x.strip() for x in f.readlines()]

f.close()

return content

src = sys.argv[1]

dst = sys.argv[2]

python if,file_with_list = sys.argv[3]

copy_files(src,dst,read_file(file_with_list))

~~~

2. 將以上代碼保存為move.py

3. 運行 $ python move.py /path/to/src/ /path/to/dst/ file.txt

4. file.txt 中定義要復制的文件名字,只要給出名字即可,不需要路徑

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/181286.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息