英文:
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????
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论