2016-12-09 13 views
0

txtファイルの内容をパスに挿入したいとします。2番目のファイルへのパス内のファイルの内容を使用

例:

私は、私は2番目のファイルのパス上のコンテンツ(08122016)を置くにはどうすればよいコンテンツ

08122016 

./path/date.txtでtxtファイルを持っていますか?このような

何か:

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

答えて

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() 
+0

いいえ、それはパスがどのように構築されるかではありません。 https://docs.python.org/3/library/os.path.html#os.path.joinが存在します。 –

+0

OK、情報ありがとう! – Soltius

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() 
0

は、あなたがそのような他の文字列に文字列を挿入することができます(のpython 2.7.12):

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

文字列の%sはコンテンツ変数に置き換えられます。

+1

Pythonには電池が付属しています:https://docs.python.org/3/library/os.path.html#os.path.join –

関連する問題