简体   繁体   English

修改指定目录(包括子文件夹)中的所有文件并将它们保存在新目录中,同时保留文件夹结构(Python)

[英]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.

相关问题 如何在不复制python中的文件夹结构的同时从文件夹(包括子文件夹)复制所有文件 - How to copy all files from a folder (including sub-folder) while not copying the folder structure in python Python:将文件从文件夹和子文件夹移动到另一个类似的目录 - Python: Move files from Folder and subfolders to another similar directory In Python I'm trying to convert all files in directory from PDF to CSV, then edit csv with Pandas before saving to new folder - In Python I'm trying to convert all files in directory from PDF to CSV, then edit csv with Pandas before saving to new folder 使用 Python 修改目录中的所有文件(循环) - Modify all files in directory using Python (Loop) 将特定文件移动到新目录,同时保持原始目录结构 - Move specific files to new directory while maintaining original directory structure Python-获取目录中所有文件和子文件夹的相对路径 - Python - Get relative path of all files and subfolders in a directory 将目录中的所有文件复制到新目录中,同时在 Python 中重命名同名文件 - Copy all files within directory into new directory while renaming files with same name in Python 在Python中,如何查找目录下的所有文件,包括子目录中的文件? - In Python, how to find all the files under a directory, including the files in subdirectories? Zip 多个文件夹(在目录中)到单个文件(在新目录中),在 Python - Zip multiple folder (within directory) to individual files (in a new directory), in Python 如何使用python读取目录的所有新文件? - How to read all new files of a directory with python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM