我正在尝试创建一个代码块来创建一系列.txt文件名。它将打开一个新文件并提示用户输入文件的文本,直到用户不再需要文件为止。
如何创建一系列文件名,例如 file1.txt,file2.txt,file3.txt 等?
答案 0 :(得分:1)
for obj in s3client.list_objects(Bucket='mybucket')['Contents']:
try:
filename = obj['Key'].rsplit('/', 1)[1]
except IndexError:
filename = obj['Key']
localfilename = os.path.join('/home/username/Downloads/', filename) # The local directory must exist.
s3client.download_file('mybucket', obj['Key'], localfilename)
答案 1 :(得分:0)
假设您事先知道需要多少文件,您可以将起始值传递给函数range()
fnum = 10
fname = 'file'
ext = '.txt'
for i in range(1, fnum + 1):
with open("{fname}{fnum}{ext}".format(fname=fname, fnum=i, ext=ext), 'w') as f:
# OR for Python 3.6+
# with open(f'{fname}{i}{ext}', 'w') as f:
print(input('User Input: '), file=f)