文件IO-求当前目录下所有普通文件的字节数总和

调用opendir,readdir,stat函数

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
    DIR *drip = opendir("请在此处输入路径");//例如:/mnt/hgfs/share/mycode/
    if(drip == NULL)
    {
        perror("目录获取失败,原因是");
    }
    struct dirent *p;
    long int sum = 0;
    while(1)
    {
        p = readdir(drip);
        if(p == NULL)
        {
            break;
        }
        struct stat sb;
        int s = stat(p->d_name,&sb);
        if(-1 == s)
        {
            perror("文件属性获取失败,原因是");
            return -1;
        }
        if ((sb.st_mode & S_IFMT) == S_IFREG) 
        {
            sum+=sb.st_size;
            printf("%s文件,文件属性为普通文件,文件有%ld个字节,此时普通文件总字节为%ld\n",p->d_name,sb.st_size,sum);
        }
        else
        {
            printf("%s文件,文件属性为非普通文件,文件有%ld个字节,不计入\n",p->d_name,sb.st_size);
        }
    }
    closedir(drip);
    return 0;
}

要根据“上下心的总字节数”(通常应为“上下行的总字节数”)来计算**上行速率**和**下行速率**,我们需要明确以下几点: - **上行字节数(Upload Bytes)**:设备发送的数据总量 - **下行字节数(Download Bytes)**:设备接收的数据总量 - **时间间隔(Δt)**:两次采样之间的时间差(单位:秒) --- ### ✅ 计算公式 > **速率 = (本次字节数 - 上次字节数) / 时间间隔** 单位换算: - 字节/秒(B/s) - 比特/秒(bps) = 字节/秒 × 8 - 常用单位:Kbps(千比特/秒)、Mbps(兆比特/秒) --- ### 🧪 示例场景 假设你每隔 1 秒采集一次网络接口的统计数据(如 Linux 的 `/proc/net/dev` 或通过 `psutil` 获取): ```python import time import psutil # 初始化上次数据 last_upload = 0 last_download = 0 last_time = time.time() print("正在监控上下行速率... 按 Ctrl+C 停止") try: while True: # 获取当前网络 I/O 统计 net_io = psutil.net_io_counters() current_upload = net_io.bytes_sent current_download = net_io.bytes_recv current_time = time.time() # 时间差 delta_time = current_time - last_time if delta_time > 0: # 计算速率(字节/秒) upload_speed_bps = (current_upload - last_upload) / delta_time download_speed_bps = (current_download - last_download) / delta_time # 转换为 Mbps(兆比特每秒) upload_speed_mbps = upload_speed_bps * 8 / 1_000_000 download_speed_mbps = download_speed_bps * 8 / 1_000_000 print(f"上行速率: {upload_speed_mbps:.2f} Mbps | 下行速率: {download_speed_mbps:.2f} Mbps") # 更新上次值 last_upload = current_upload last_download = current_download last_time = current_time time.sleep(1) # 每秒更新一次 except KeyboardInterrupt: print("\n监控结束。") ``` --- ### 🔍 代码解释 - `psutil.net_io_counters()`:获取系统级别的网络收发字节数- 我们记录前后两次的字节数差值,除以时间差,得到平均速率。 - 乘以 8 将字节转为比特,再除以 1,000,000 得到 Mbps。 - 使用 `time.time()` 精确测量时间间隔,避免固定 sleep 导致误差。 --- ### ⚠️ 注意事项 1. **多网卡问题**:`net_io_counters()` 返回的是所有网卡总和。如果只想监控某个接口(如 `eth0`, `wlan0`),使用 `psutil.net_io_counters(pernic=True)`。 2. **单位一致性**:注意 B(字节)与 b(比特)的区别。 3. **初始值处理**:第一次计算时速率可能不准,可跳过或忽略首帧。 4. **采样频率**:建议 0.5~2 秒采样一次,太频繁会导致波动大。 --- ### ✅ 扩展:只监控特定网卡(例如 wlan0) ```python io = psutil.net_io_counters(pernic=True) if 'wlan0' in io: stats = io['wlan0'] tx_bytes = stats.bytes_sent rx_bytes = stats.bytes_recv ``` 然后用同样的方式计算速率。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值