python - 是否可以在 Python 中创建一个带有函数的文件?

标签 python function file

我正在尝试用 python 中的函数创建一个文件,但我不知道如何做。这就是我所拥有的,也是我所需要的。

def file_maker():
  file_number = input("What number player are you? ")
  #Insert however you make a file in code, naming it ('inventory.' + filenumber + '.txt')

我只需要知道如何启动文件制作过程。我试着用谷歌搜索它,但唯一出现的是如何访问不同文件中的函数。我是一个业余程序员,欢迎任何和所有建议。感谢您的宝贵时间。

最佳答案

def file_maker():
  file_number = input("What number player are you? ")
  with open("inventory.%s.txt" % file_number, "w") as f:
    # Do whatever you need with the file using the 'f' to refer to the file object
    pass # in case you don't want to do anything with the file, but just create it

在此处阅读有关 open 功能的更多信息:Open function

仅供引用,如果文件已经存在,这将覆盖该文件。

关于python - 是否可以在 Python 中创建一个带有函数的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59165636/

相关文章:

c++ - SQLite 文件锁定和 DropBox

python - 为什么我的脚本不写入文件?

python - 用 Pandas 和 Group By 绘制堆叠直方图

python - 如何使用 Ubuntu 在 Docker 中持久化 'ln'

c - 有没有办法将单独的 1's and 0' 写入 C 中的 .bin 文件?

function - 在 rust trait 中实现构造函数

jquery - 如何使用 jQuery 在第二次单击时反转 CSS 动画

Python 2.7 从登录网站抓取网页

python - Python屏幕共享

JavaScript 将参数传递给按钮 onclick 函数