day09-文件搜索与读取

了解网站部署

1.nginx小试牛刀

yum install  nginx -y 


2.简单了解,给你的机器,配置一个软件仓库(360软件大师,点点点,下载各种工具,它的软件,还携带了一堆广告)

配置阿里云的软件仓库
https://developer.aliyun.com/mirror/

下载资源,且指定到某一个路径下

centos7源

#第一种方法
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

#第二种方法
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

epel源

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

3.既然机器有了软件仓库,即可下载各种应用了

yum install nginx -y

4. 就可以启动该应用

systemctl start nginx 

5.程序运行了,如何判断他运行? 

windows 
    任务管理器 
        查看进程,就是一个个运行中的软件,在消耗系统的资源
        
linux
    任务管理器
        查看进程
            查看nginx这个程序的进程    
            
                执行ps命令 

仅仅查看这个机器上的 nginx进程信息,是否存在

ps -ef | grep nginx 

6. 一个网站的运行,默认端口是 http的80端口 

用你的客户端,浏览器去访问, 
10.96.0.134:80即可  (虚拟机的网卡地址 + 端口)

7.注意关闭服务器的防火墙

iptables -F

setenforce 0

 *8.停止这个网站服务,思路是?

systemctl stop nginx

重启nginx服务 

systemctl restart nginx

8.可以修改该软件的配置了,比如网站首页的内容

完成1分钟,做一个dnf官网

下载该网址的首页html文件
wget https://dnf.qq.com/main.shtml

移动,拷贝该文件,到你的nginx网站目录下, 它会自动识别

移动 /opt/main.shtml  到/usr/share/nginx/html/ 目录下,且重命名为index.html

[root@localhost opt]# mv /opt/main.shtml /usr/share/nginx/html/index.html
mv: overwrite ‘/usr/share/nginx/html/index.html’? y

9.当你修改了网站的前端页面,无须重启,网站自动更新

http://10.96.0.134/

文件管理篇命令二

cat命令

1.cat 适合读取小文件,不能读取大文件,一次性将文件内容全部读取到内存中,且输出到屏幕上

查看nginx软件的配置文件(前提是你安装了该软件,linux默认安装的软件,配置文件会自动写到/etc目录下)

[root@localhost opt]# cat /etc/nginx/nginx.conf
查看系统的用户信息有哪些

[root@localhost opt]# cat /etc/passwd

2.cat 添加一个功能参数  -n 显示行号

[root@localhost opt]# cat -n  /etc/passwd

3. cat不适合读取大文件,显示也不友好

cat  /var/log/messages

4. 还可以连续读取多个文件,并且显示三文件一共有多少行

[root@localhost opt]# cat -n  doupo.txt   hehe.txt   hello_python.txt

5. 结合重定向符号使用

>   重定向覆盖输出符  ,数据从左边,覆盖写入到右边 
<   重定向覆盖输入符,数据从右边,覆盖写入到左边
>>  重定向追加输出符, ,数据从左边,追加写入到右边 
<<  重定向覆盖输入符,数据从右边,追加写入到左边

cat  doupo.txt   hehe.txt   hello_python.txt  > ./three_files.txt 

6.追加古诗

[root@localhost opt]# echo 春晓 > 春晓.txt
[root@localhost opt]# echo 春眠不觉晓 >> 春晓.txt
[root@localhost opt]# echo 处处蚊子咬>> 春晓.txt
[root@localhost opt]# echo 越来讽喻诗>> 春晓.txt
[root@localhost opt]# echo 滑落至多为>> 春晓.txt
[root@localhost opt]# cat 春晓.txt 
春晓
春眠不觉晓
处处蚊子咬
越来讽喻诗
滑落至多为

7.写入多行数据

[root@localhost opt]# cat  >>古诗2.txt <<EOF
> 嗯嗯嗯
> 呃呃呃,曲项向天歌
> 白毛浮绿水
> 红掌拨清波
> EOF

[root@localhost opt]# cat 古诗2.txt 
嗯嗯嗯
呃呃呃,曲项向天歌
白毛浮绿水
红掌拨清波

注意:如果后面写进的文档没有会自动创建

8.cat证明文件存在空行的办法


-b  只会对有内容的行,显示其行号,空行不显示
-E  在linux文件中,每一行的结束,默认会添加一个你看不到的,特殊符号 '$'  ,表示是该行的结尾

#查看文件的字符数量有多少

[root@localhost opt]# cat -b doupo.txt 

tac命令

将文件从后,向前,倒着查看

[root@localhost opt]# cat  -n hehe.txt 
     1	yuchao
     2	yejingyang
     3	chenliangliang
[root@localhost opt]# 
[root@localhost opt]# 
[root@localhost opt]# tac hehe.txt 
chenliangliang
yejingyang
yuchao

ls和sl

ls 查看文件夹下内容

安装这个sl命令
yum install sl -y

sl是一个小火车命令

more和less命令

more和cat都是一次性读取所有内容到内存,不适合读取大文件,占资源

less命令是显示多少文本,消耗多少内存,省资源。

空格--------翻篇

回车--------下一行

head和tail命令

head 

作用:查看文件的默认前n行

查看文件的默认前10行

[root@localhost opt]# head  doupo.txt

[root@localhost opt]# head -5  文件   # 查看文件的前5行

tail 命令

作用:查看文件的默认后n行

查看文件的后默认10行

tail -5  文件  #表示查看文件的后5行

tail -f 

tail有一个重点命令,叫做实时刷新文件内容

-f  跟踪文件内容变化,但是需要文件正常退出后,可见,最常用的也就是小写的f,检测程序的日志变化(程序代码,追加新内容到文件中的)

结合tail和上午安装的nginx练习日志查看

1.用tail 检测nginx的访问日志
[root@localhost opt]# tail -f  /var/log/nginx/access.log 


2.用浏览器,访问nginx的页面即可,不断刷新,不断出现新的

 tail -F

能够对文件进行刷新读取,即使该文件不存在,也可以检测

wc命令

统计文件的行

1.wc命令,统计文件内有多少行,有一个回车,是一个空行

[root@localhost opt]# wc -l doupo.txt 
32 doupo.txt

[root@localhost opt]# ll

 2. wc -w 统计文件内的单词数

[root@localhost opt]# wc -w hehe.txt 

11 hehe.txt

du命令

作用:查看文件或目录(会递归显示子目录)占用磁盘空间大小

语法:du [参数选项] 文件名或目录名

参数:

-s :summaries,只显示汇总的大小,统计文件夹的大小

[root@localhost opt]# du -s .
105072KB	

-h:表示以高可读性的形式进行显示,如果不写-h,默认以KB的形式显示文件大小

linux的文件系统,对文件最小管理单位是4kb算起。

[root@localhost opt]# du -h *
4.0K	doupo.txt
4.0K	hehe.txt
4.0K	hello_python.txt
4.0K	index.txt
4.0K	three_files.txt
26M	xixi
26M	xixi1
26M	xixi2
26M	xixi3
4.0K	古诗2.txt
4.0K	呵呵.txt
4.0K	春晓.txt
4.0K	蔡旭困.txt

2.显示文件夹的大小

[root@localhost /]# du -h /opt
103M	/opt

3.发现机器磁盘空间不够了,你领导让你看看日志目录是不是太大了

du -sh  /var/log

4.查看nginx软件的日志目录,容量是多少

[root@localhost log]# du -sh /var/log/nginx

32K	/var/log/nginx

find命令

windows下的强大搜索工具

everything工具

linux的工具是谁?---find命令。

find是递归查找

语法:

find   从哪找   -name  "你要找什么"

[root@localhost ~]# find  /  -name 'doupo.txt'

/opt/doupo.txt

缩小搜索范围,从/opt开始找

[root@localhost ~]# find  /opt  -name 'doupo.txt'
/opt/doupo.txt

找到机器上所有的 doupo.txt

[root@localhost ~]# find  /  -name 'doupo.txt'
/etc/doupo.txt
/tmp/doupo.txt
/usr/local/doupo.txt
/home/doupo.txt
/opt/doupo.txt
[root@localhost ~]# 

模糊查找,找出/var下所有的log文件  nginx.log  mysql.log   yuchao.log    *.log 

[root@localhost ~]# find /var  -name '*.log'

这是限制查找文件类型的:


# -type  f   找到文本类型的数据 


# -type  d    找到文件夹类型的数据
 

找出机器上所有的 doupo.txt 【文件,文本文件,可以被cat的文件】

find /    -type f     -name   'doupo.txt' 

找出系统上超过20M的压缩包(自行查阅)

[root@server ~]#find / -type f -size 20M -name "*.tar.gz"

-o # 找出多种类型的文件

<think>我们需要遍历当前目录(或指定目录)下的所有.day文件,然后对每个文件调用day_to_csv函数进行转换。 可以修改主程序部分,使用glob模块来匹配所有的.day文件,然后循环处理。 注意:为了避免覆盖,我们可以为每个输入文件生成对应的输出文件名(例如:将输入文件名中的.day替换为.csv,或者添加前缀/后缀) 以下是修改后的主程序部分:</think>要批量将目录下所有`.day`文件转换为CSV文件,可以使用以下Python脚本。该脚本会自动遍历指定目录,处理所有匹配的文件: ```python import os import glob import struct import csv from datetime import datetime def read_day_file(file_path): """读取单个.day文件并解析为字典列表""" data = [] try: with open(file_path, 'rb') as f: while True: chunk = f.read(32) if len(chunk) < 32: break # 解包数据:日期,开盘,最高,最低,收盘,成交量,成交额,保留字段 date_int, open_price, high, low, close, volume, amount, _ = struct.unpack('8i', chunk) # 转换日期格式 date_str = str(date_int) date_obj = datetime.strptime(date_str, '%Y%m%d').date() # 添加解析后的记录 data.append({ 'date': date_obj, 'open': open_price / 100.0, # 分→元 'high': high / 100.0, 'low': low / 100.0, 'close': close / 100.0, 'volume': volume, # 成交量(股) 'amount': amount / 100.0 # 成交额(分→元) }) except Exception as e: print(f"读取 {file_path} 时出错: {e}") return [] return data def convert_day_to_csv(input_file, output_file): """将单个.day文件转换为CSV格式""" stock_data = read_day_file(input_file) if not stock_data: return False try: with open(output_file, 'w', newline='', encoding='utf-8-sig') as csvfile: fieldnames = ['date', 'open', 'high', 'low', 'close', 'volume', 'amount'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for row in stock_data: row['date'] = row['date'].strftime('%Y-%m-%d') writer.writerow(row) return True except Exception as e: print(f"写入 {output_file} 时出错: {e}") return False def batch_convert_day_to_csv(directory=".", output_dir="converted_csv"): """ 批量转换目录中的所有.day文件为CSV :param directory: 包含.day文件的目录 :param output_dir: 输出CSV文件的目录 """ # 确保输出目录存在 os.makedirs(output_dir, exist_ok=True) # 获取所有.day文件 day_files = glob.glob(os.path.join(directory, "*.day")) total_files = len(day_files) if total_files == 0: print(f"在目录 {directory} 中未找到.day文件") return print(f"找到 {total_files} 个.day文件,开始转换...") # 处理每个文件 success_count = 0 for i, day_file in enumerate(day_files): # 生成输出文件名 filename = os.path.basename(day_file) csv_filename = os.path.splitext(filename)[0] + ".csv" csv_path = os.path.join(output_dir, csv_filename) # 转换文件 print(f"处理文件 ({i+1}/{total_files}): {filename} → {csv_filename}") if convert_day_to_csv(day_file, csv_path): success_count += 1 print(f"\n转换完成! 成功: {success_count}/{total_files} 个文件") print(f"所有CSV文件已保存到: {os.path.abspath(output_dir)}") # 使用示例 if __name__ == "__main__": # 设置输入目录(默认为当前目录) input_directory = "." # 可修改为其他目录 # 设置输出目录(默认为当前目录下的converted_csv文件夹) output_directory = "converted_csv" # 执行批量转换 batch_convert_day_to_csv(input_directory, output_directory) ``` ### 使用说明: 1. **默认设置**: - 脚本会搜索**当前目录**下的所有`.day`文件 - 转换后的CSV文件会保存在`converted_csv`子目录中 - 输出文件输入文件相同,仅扩展名改为`.csv` 2. **自定义设置**: - 修改`input_directory`变量指定.day文件所在目录 - 修改`output_directory`变量指定CSV输出目录 ```python # 示例:指定不同目录 input_directory = r"D:\stock_data\day_files" # Windows路径 # 或 input_directory = "/home/user/stock_data/day_files" # Linux/Mac路径 ``` 3. **运行脚本**: - 将脚本保存为`convert_day_files.py` - 在命令行运行:`python convert_day_files.py` ### 输出示例: ``` 找到 15 个.day文件,开始转换... 处理文件 (1/15): sz000001.day → sz000001.csv 处理文件 (2/15): sz000002.day → sz000002.csv ... 转换完成! 成功: 15/15 个文件 所有CSV文件已保存到: C:\project\converted_csv ``` ### 高级功能扩展: 1. **递归处理子目录**: 在`batch_convert_day_to_csv`函数中添加递归搜索: ```python day_files = glob.glob(os.path.join(directory, "**", "*.day"), recursive=True) ``` 2. **保留原始目录结构**: ```python # 在batch_convert_day_to_csv函数中 rel_path = os.path.relpath(day_file, directory) csv_path = os.path.join(output_dir, os.path.splitext(rel_path)[0] + ".csv") os.makedirs(os.path.dirname(csv_path), exist_ok=True) ``` 3. **添加进度条**: 安装`tqdm`库:`pip install tqdm` 然后修改循环: ```python from tqdm import tqdm for day_file in tqdm(day_files, desc="转换进度"): ``` 4. **并行处理加速**: 使用`concurrent.futures`实现并行处理: ```python from concurrent.futures import ThreadPoolExecutor with ThreadPoolExecutor(max_workers=4) as executor: futures = [executor.submit(convert_day_to_csv, day_file, os.path.join(output_dir, os.path.basename(day_file).replace('.day', '.csv'))) for day_file in day_files] success_count = sum(1 for f in futures if f.result()) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

激进的猴哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值