[英]Modify all files in specified directory (including subfolders) and saving them in new directory while presevering folder structure (Python)
(I'm new to python so please excuse the probably trivial question. I tried my best looking for similar issues but suprisingly couldn't find someone with the same question.) (我是 python 的新手,所以请原谅这个可能微不足道的问题。我尽力寻找类似的问题,但令人惊讶的是找不到有同样问题的人。)
I'm trying to build a simple static site generator in Python.我正在尝试在 Python 中构建一个简单的 static 站点生成器。 The script should take all.txt files in a specific directory (including subfolders), paste the content of each into a template.html file and then save all the newly generated.html files into a new directory while recreating the folder structure of the original directory.
该脚本应将所有.txt文件放在特定目录(包括子文件夹)中,将每个文件的内容粘贴到模板.html文件中,然后将所有新生成的.html文件保存到新目录中,同时重建原始文件夹结构目录。
So for I got the code which does the conversion itself for a single file but I'm unsure how to do it for multiple files in a directory.因此,我得到了为单个文件进行转换的代码,但我不确定如何为目录中的多个文件进行转换。
with open('template/page.html', 'r') as template:
templatedata = template.read()
with open('content/content.txt', 'r') as content:
contentdata = content.read()
pagedata = templatedata.replace('!PlaceholderContent!', contentdata)
with open('www/content.html', 'w') as output:
output.write(pagedata)
To manipulate files and directories, you will need to import some system functionalites under the built-in module os
.要操作文件和目录,您需要在内置模块
os
下导入一些系统功能。
import os
The functionalities under the os
module include: os
模块下的功能包括:
Listing the content of a directory:列出目录的内容:
path_to_template_dir = 'template/' template_files = os.listdir(path_to_template_dir) print(template_files) # Outputs: ['page.html']
Creating a directory (If it does not already exist):创建一个目录(如果它不存在):
path_to_output_dir = 'www/' try: os.mkdir(path_to_output_dir) except FileExistsError as e: print('Directory exists:', path_to_output_dir)
And since you know the names of the directories you want to use, and using these two functions, you now know the names of the files you want to use and generate, you can now concatenate the name of each file to the names of its directories to create the string str
of the final file path, which you can then open()
for reading and/or writing.并且由于您知道要使用的目录的名称,并且使用这两个函数,您现在知道要使用和生成的文件的名称,您现在可以将每个文件的名称连接到其目录的名称创建最终文件路径的字符串
str
,然后您可以使用open()
进行读取和/或写入。
It's hard to give a perfect code example for your question since the logic of how you want to manipulate each of the template and content file is missing, but here is an example for writing a file inside the newly created directory:很难为您的问题提供完美的代码示例,因为缺少您希望如何操作每个模板和内容文件的逻辑,但这里有一个在新创建的目录中编写文件的示例:
path_to_output_file = path_to_output_dir + 'content.html'
with open(path_to_output_file, 'w') as output:
output.write('Content')
And an example for reading all the template files inside the template/
directory and then printing them to the screen.还有一个读取
template/
目录中的所有模板文件,然后将它们打印到屏幕上的示例。
for template_file in template_files:
path_to_template_file = path_to_template_dir + template_file
with open(path_to_template_file, 'r') as template:
print(template.read())
In the end, manipulating files is all about creating the path string you want to read from or write to, and then accessing it.最后,操作文件就是创建要读取或写入的路径字符串,然后访问它。
Anymore functionalities you might need (for example: checking if a path is a file os.path.isfile()
or if it's for a directory os.path.isdir()
can be found under the os
module.您可能需要的更多功能(例如:检查路径是文件
os.path.isfile()
还是用于目录os.path.isdir()
可以在os
模块下找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.