[英]How to set a name of creating file from variable?
I need to create a file with the name provided by the user as input.我需要使用用户提供的名称作为输入创建一个文件。 For example, If the user-provided value
hello
then the file name should be hello.txt
, here is a sample code to create a new file.例如,如果用户提供值
hello
那么文件名应该是hello.txt
,这里是创建一个新文件的示例代码。
import os
recipeFileName = input()
currentRecipe = open("fileName.txt", "x")
Since it is a text file so pls use w
instead of x
.由于它是一个文本文件,所以请使用
w
而不是x
。
recipeFileName = input("Input file Name: ")
currentRecipe = open( recipeFileName + '.txt' , "w")
# insert into file
currentRecipe.close()
open
can be used to create a file.(you can just replace the string "fileName.txt" with the veriable containing the string and add '.txt' for the extension open
可用于创建文件。(您可以将字符串“fileName.txt”替换为包含该字符串的可验证文件,并为扩展名添加“.txt”
recipeFileName = input()
currentRecipe = open(recipeFileName+'.txt' , "x")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.