以下是一段代码。该脚本从“ Test”文件夹中获取输入文件,运行一个函数,然后在“ Results”文件夹中输出相同名称的文件(即"Example_Layer.shp")
,如何设置它,以便输出文件改为读取"Example_Layer(A).shp"
?
#Set paths
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"
def run():
#Set definitions
input = path_res + "/" + "input.shp"
output = path_res + "/" + fname
#Set current path to path_dir and search for only .shp files then run function
os.chdir(path_dir)
for fname in glob.glob("*.shp"):
run_function, input, output
run()
要回答您的问题:
我如何设置它,以便输出文件改为显示“ Example_Layer(A).shp”
您可以使用shutil.copy将文件复制到新目录,并"(A)"
使用os.path.join
连接路径和新文件名的方式在每个文件名中添加一个:
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"
import os
import shutil
def run():
os.chdir(path_dir)
for fname in glob.glob("*.shp"):
name,ex = fname.rsplit(".",1) # split on "." to rejoin later adding a ("A")
# use shutil.copy to copy the file after adding ("A")
shutil.copy(fname,os.path.join(path_res,"{}{}{}".format(name,"(A)",ex)))
# to move and rename in one step
#shutil.move(fname,os.path.join(path_res,"{}{}{}".format(name,"(A)",ex)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句