读取files目录下所有txt文件,并汇总保存txt文件名称及内容信息
时间: 2024-05-06 10:21:51 浏览: 14
好的,可以使用Python的os和glob模块来实现。代码如下:
```python
import glob
import os
# 获取当前文件所在目录下的所有txt文件路径
path = os.path.join(os.getcwd(), "files/*.txt")
txt_files = glob.glob(path)
# 汇总保存txt文件名称及内容信息
txt_dict = {}
for file in txt_files:
with open(file, "r") as f:
content = f.read()
txt_dict[os.path.basename(file)] = content
# 打印输出txt文件信息
for file, content in txt_dict.items():
print(f"文件名: {file}")
print(f"文件内容: {content}\n")
```
请问还有其他问题吗?
相关问题
C# 从指定文件夹下读取所有TXT文件 并逐行读取TXT文件的内容
以下是C#从指定文件夹下读取所有TXT文件并逐行读取TXT文件内容的示例代码:
```csharp
using System;
using System.IO;
class Program
{
static void Main()
{
string folderPath = @"C:\YourFolderPath"; // 指定文件夹路径
string[] txtFiles = Directory.GetFiles(folderPath, "*.txt"); // 获取文件夹下所有的TXT文件
foreach (string filePath in txtFiles)
{
Console.WriteLine("文件名: {0}", Path.GetFileName(filePath));
using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine("本行的数据为: {0}", line);
}
}
Console.WriteLine();
}
Console.ReadLine();
}
}
```
这段代码首先使用`Directory.GetFiles()`方法获取指定文件夹下所有的TXT文件路径。然后,使用`StreamReader`类逐行读取每个TXT文件的内容,并将每行数据打印到控制台上。
请注意,你需要将`folderPath`变量替换为你想要读取的文件夹路径。
用python读取目录下的.java文件并转换成txt保存
你可以使用Python的os和io模块,以及JavaLexer和Token的pygments模块来实现这个功能。
首先,你需要安装pygments模块。你可以使用以下命令来安装:
```python
pip install pygments
```
然后,你可以使用以下代码来读取目录下的所有.java文件,并将其转换为txt文件:
```python
import os
from pygments.lexers import JavaLexer
from pygments.token import Token
import io
directory = "/path/to/directory"
for filename in os.listdir(directory):
if filename.endswith(".java"):
with io.open(filename, 'r', encoding='utf-8') as f:
code = f.read()
tokens = JavaLexer().get_tokens(code)
with io.open(filename.replace('.java','.txt'), 'w', encoding='utf-8') as f:
for token in tokens:
if token[0] in Token.Comment or token[0] in Token.Text:
continue
f.write(token[1])
```
请将`/path/to/directory`替换为你的目录路径。这段代码将读取目录中所有的.java文件,并将它们转换为.txt文件。在这个过程中,代码将使用JavaLexer将Java代码转换为令牌,然后将令牌转换为纯文本,并将其保存到.txt文件中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)