Home  >  Article  >  Backend Development  >  How to use variables in python file storage path

How to use variables in python file storage path

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-18 09:38:1510034browse

在python项目中,需要批量ocr图片后,批量生成同名的txt文件,存储文件需要使用路径,一般是path =r'd:\folder\filename.txt',使用变量怎么代替路径呢?

相关推荐:《python视频教程

How to use variables in python file storage path

步骤如下:

直接使用变量就可以了。

path=variable

How to use variables in python file storage path

因为r'()'符号的作用是使用原始字符串,它会改变引号内字符转义的规则。如果您只是从另一个变量中获取值,则不需要r写入字符串。

代码:

filelist=['test1.jpg','test2.jpg']
for filename in filelist:
   filenamev= 'D:\\baoan-nlt\\' + filename    
   savepath = filenamev[:-4]+'.txt'

How to use variables in python file storage path

The above is the detailed content of How to use variables in python file storage path. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn