【问题标题】:Create a file with an input使用输入创建文件
【发布时间】:2023-03-13 12:22:01
【问题描述】:

我对打开的文件有疑问。首先我需要设计我的文件的名称,然后它应该在格式为 .txt 的块状注释中创建一个文件。另外,我必须使用 %less 才能打印文件。

这是我的代码:

    def file():
       name=str(input("write the name of your file: ")) 
       content=str(input("write : "))

       with open(name+".txt" , "w") as newfile:
         newfile.write(content)
       %less name.txt

    file()
    leer_libro("name.txt") #it counts my letters of each line

【问题讨论】:

  • 你有什么问题?如果您回答 name,您的代码应该可以工作。
  • 它不起作用
  • 这还不够详细。
  • 如果你想使用普通的 Unix 工具,你应该用换行符结束文件。

标签: python file input with-statement getopenfilename


【解决方案1】:

您必须传递到with 的绝对路径。 示例:

with open("D:/PYTHON/03_Coding_Rule/name.txt", "a") as fi:

请输入绝对路径

你有这个问题吗?

【讨论】:

    猜你喜欢
    • 2013-07-08
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多