在第二个文件的路径中使用文件的内容

时间:2016-12-09 09:15:33

标签: python

我想在路径中插入txt文件的内容。

示例:

我在./path/date.txt中有一个内容为

的txt文件
08122016

如何将内容(08122016)放在第二个文件的路径上?

这样的事情:

s = open('/erp/date/**date.txt content**').read()

3 个答案:

答案 0 :(得分:2)

使用os.path.join

import os

with open(r'./path/date.txt', 'rt') as input_file:
    data = input_file.read()
with open(os.path.join('/erp/date', data), 'rt') as input_file2:
    data2 = input_file2.read()

答案 1 :(得分:0)

#open the date file
f = open("./path/date.txt", 'r')
#read the content
content=f.read()
#close file
f.close()

#insert date in path
s=open("/erp/date/"+str(content)).read()

答案 2 :(得分:0)

您可以将字符串插入其他字符串(python 2.7.12):

path = 'home/user/path/%s' % content

字符串中的%s将被内容变量替换。