txtファイルの内容をパスに挿入したいとします。2番目のファイルへのパス内のファイルの内容を使用
例:
私は、私は2番目のファイルのパス上のコンテンツ(08122016
)を置くにはどうすればよいコンテンツ
08122016
と./path/date.txt
でtxtファイルを持っていますか?このような
何か:
s = open('/erp/date/**date.txt content**').read()
txtファイルの内容をパスに挿入したいとします。2番目のファイルへのパス内のファイルの内容を使用
例:
私は、私は2番目のファイルのパス上のコンテンツ(08122016
)を置くにはどうすればよいコンテンツ
08122016
と./path/date.txt
でtxtファイルを持っていますか?このような
何か:
s = open('/erp/date/**date.txt content**').read()
#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()
使用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()
は、あなたがそのような他の文字列に文字列を挿入することができます(のpython 2.7.12):
path = 'home/user/path/%s' % content
文字列の%sはコンテンツ変数に置き換えられます。
Pythonには電池が付属しています:https://docs.python.org/3/library/os.path.html#os.path.join –
いいえ、それはパスがどのように構築されるかではありません。 https://docs.python.org/3/library/os.path.html#os.path.joinが存在します。 –
OK、情報ありがとう! – Soltius