遍历“C:\Users\84297\Desktop\爬虫\data”路径中的txt文件
时间: 2023-03-29 09:04:33 浏览: 60
你可以使用以下代码来遍历指定路径中的txt文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
int main() {
DIR *dir;
struct dirent *ent;
char *path = "C:\\Users\\84297\\Desktop\\爬虫\\data";
if ((dir = opendir(path)) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strstr(ent->d_name, ".txt") != NULL) {
printf("%s\n", ent->d_name);
}
}
closedir(dir);
} else {
printf("无法打开目录\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
相关问题
遍历“C:\Users\84297\Desktop\爬虫\data”路径中的txt文件并进行分词的代码怎么写
你可以使用Python中的os和jieba库来实现这个功能。具体代码如下:
```python
import os
import jieba
# 遍历指定路径下的所有txt文件
def traverse_folder(path):
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith('.txt'):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 对文件内容进行分词
seg_list = jieba.cut(content)
print('/'.join(seg_list))
# 调用函数进行遍历和分词
traverse_folder(r'C:\Users\84297\Desktop\爬虫\data')
```
这段代码会遍历指定路径下的所有txt文件,并对文件内容进行分词,最后输出分词结果。
遍历“C:\Users\84297\Desktop\爬虫\data”路径中的txt文件并进行分词,在“C:\Users\84297\Desktop\爬虫\pp”下生成对应的文本文件
你好,我可以回答这个问题。您可以使用Python中的os和jieba库来实现这个功能。具体步骤如下:
1. 导入os和jieba库
import os
import jieba
2. 遍历文件夹并进行分词
def cut_words(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
words = jieba.cut(content)
return ' '.join(words)
def traverse_folder(folder_path):
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.txt'):
file_path = os.path.join(root, file)
words = cut_words(file_path)
save_path = os.path.join('C:\\Users\\84297\\Desktop\\爬虫\\pp', file.replace('.txt', '_cut.txt'))
with open(save_path, 'w', encoding='utf-8') as f:
f.write(words)
3. 调用函数
traverse_folder('C:\\Users\\84297\\Desktop\\爬虫\\data')
这样就可以遍历“C:\Users\84297\Desktop\爬虫\data”路径中的txt文件并进行分词,在“C:\Users\84297\Desktop\爬虫\pp”下生成对应的文本文件了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
网页内源代码模板如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"> <head> </head> <body>[talkid:138031370]2014年4月20日 03:55:45 , 111222 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031371]2014年4月20日 04:45:45 , 111222 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频
() [talkid:138031372]2014年4月20日 04:55:45 , 111222 向 123456 发送 图片 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片 () </body> </html> 利用python爬虫,打开C:/Users/test/Desktop/DIDItest文件夹下多个文件夹内的html文件源代码,并爬取源代码中的ID、时间、发送号码、接收号码、发送内容,如果发送内容不为文本,则提取文件所在链接地址,并将爬取的内容写入csv中![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
利用python爬虫,提取C:/Users/test/Desktop/DIDItest文件夹下多个文件内的html文件源代码,并提取源代码中的ID、时间、发送号码、接收号码、发送内容,如果发送内容为音频则提取音频所在位置,反之则保留发送内容,并将爬取的内容写入csv中 网页内源代码如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"> <head> </head> <body>[talkid:138031361]2014年4月20日 03:55:45 , 434343 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031362]2014年4月20日 04:45:45 , 434343 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频
() [talkid:138031363]2014年4月20日 04:55:45 , 434343 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片 () [talkid:138031364]2014年4月20日 05:55:45 , 434343 向 3234221 发送 我们已经是好友了,开始聊天吧! () [talkid:138031365]2014年4月20日 06:55:45 , 434343 向 1359075 发送 我们已经是好友了,开始聊天吧! () </body> </html>![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
网页内源代码模板如下: <body> [talkid:138031370]2014年4月20日 03:55:45 , 111222 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031371]2014年4月20日 04:45:45 , 111222 向 123456 发送 音频 :[音频 () [talkid:138031372]2014年4月20日 04:55:45 , 111222 向 123456 发送 图片 :[图片 () [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[音频 () [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[图片 () [talkid:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已经是好友了,开始聊天吧! () </body> </html> 利用python爬虫,打开C:/Users/test/Desktop/DIDItest文件夹下所有文件夹内的html文件源代码,并提取每一行<body>之间的数据,剔除 ()
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
利用python爬虫,提取C:/Users/test/Desktop/DIDItest文件夹下每个文件夹内的html文件源代码,并提取源代码中的ID、时间、发送号码、接收号码、发送内容,如果发送内容为音频则提取音频所在位置,反之则保留发送内容,并将爬取的内容写入csv中 网页内源代码如下: <html> <meta http-equiv="Content=-Type" content="text/html; charset=utf-8"> <head> </head> <body>[talkid:138031361]2014年4月20日 03:55:45 , 434343 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031362]2014年4月20日 04:45:45 , 434343 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.m4a"]>音频
() [talkid:138031363]2014年4月20日 04:55:45 , 434343 向 123456 发送 音频 :[<ahref="files/f/f123fsasfsfsjdfrhf_n.jpg"]>图片 () [talkid:138031364]2014年4月20日 05:55:45 , 434343 向 3234221 发送 我们已经是好友了,开始聊天吧! () [talkid:138031365]2014年4月20日 06:55:45 , 434343 向 1359075 发送 我们已经是好友了,开始聊天吧! () </body> </html>![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
网页内源代码模板如下: <body> [talkid:138031370]2014年4月20日 03:55:45 , 111222 向 232323 发送 我们已经是好友了,开始聊天吧! () [talkid:138031371]2014年4月20日 04:45:45 , 111222 向 123456 发送 音频 :[音频 () [talkid:138031372]2014年4月20日 04:55:45 , 111222 向 123456 发送 图片 :[图片 () [talkid:137031382]2014年4月22日 04:45:45 , 2323234 在群 20011 中发言 音频 :[音频 () [talkid:137031383]2014年4月23日 04:55:45 , 2323234 在群 20011 中发言 图片 :[图片 () [talkid:137031384]2014年4月24日 05:55:45 , 2323234 在群 20011 中发言 我们已经是好友了,开始聊天吧! () </body> </html> 利用python爬虫,打开C:/Users/test/Desktop/DIDItest文件夹下所有文件夹内的html文件源代码,
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)