在PyCharm中如何一键在项目文件夹中创建多个带枚举的.py(Python)文件?

huangapple go评论62阅读模式
英文:

In PyCharm how to create multiple .py(Python) files with enumeration in Project Folder with one click?

问题

I'm solving programming problems from one book. I'm trying to post all answers to the problems from that book on github after solving all of them in Python. I always give enumeration to names of problems, for example: 1.py, 2.py etc...
But that's very time consuming is there anyway that I can create multiple python files with one click giving start and range and name them by iteration index, for example:
For i in range(1, 41):
create i.py file "in some location"
??????

Is there an easy way to these enumeration of multiple files????

英文:

在PyCharm中如何一键在项目文件夹中创建多个带枚举的.py(Python)文件?

Now, I'm solving programming problems from one book. I'm trying to post all answers to the problems from that book on github after solving all of them in Python. I always give enumeration to names of problems, for example: 1.py, 2.py etc...
But that's very time consuming is there anyway that I can create multiple python files with one click giving start and range and name them by iteration index, for example:
For i in range(1, 41):
create i.py file "in some location"
??????

Is there a easy way to these enumeration of multiple files????

答案1

得分: 1

你可以编写一个Python代码来实现这个功能。
您还可以为其提供自定义的范围和所需的目录路径作为参数。

以下是示例代码:

import os

def create_python_files(start, end, directory):
    for i in range(start, end + 1):
        filename = f"{i}.py"
        filepath = os.path.join(directory, filename)
        with open(filepath, "w") as file:
            # 如果需要,可以向文件写入任何初始内容
            pass
        print(f"已创建文件:{filepath}")

# 示例用法
start = 1
end = 40
directory = "/path/to/directory"  # 提供所需的目录路径

create_python_files(start, end, directory)

如果您需要进一步的帮助,请告诉我。

英文:

You can write a python code for it.
You can also give it a custom range, and a desired directory path as an argument.

Here's the sample code:

import os

def create_python_files(start, end, directory):
    for i in range(start, end + 1):
        filename = f"{i}.py"
        filepath = os.path.join(directory, filename)
        with open(filepath, "w") as file:
            # Write any initial content to the file if needed
            pass
        print(f"Created file: {filepath}")

# Example usage
start = 1
end = 40
directory = "/path/to/directory"  # Provide the desired directory path

create_python_files(start, end, directory)

答案2

得分: 0

我认为这是你想要的,但我可能错了

for i in range(0, 5):
    f = open(str(i) + '.py', 'x')
    f.close()
英文:

I think this is what you want but I may be wrong

for i in range(0, 5):
    f = open(str(i) + '.py', 'x')
    f.close()

huangapple
  • 本文由 发表于 2023年7月11日 04:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定