C++与python文件系统对比
这篇文章是 C++17 filesystem 与 Python os.path/os/shutil 的对照式实操笔记,覆盖从路径判断到目录递归操作的常见文件系统任务。对照范围与组织方式文章先给出功能映射表,把两门语言中常用文件系统 API 一一对齐。主体按三组主题展开:filesystem::path vs os.pa…
C++17 和 python 中好用的文件操作 | filesystem | os | shutil | C++ 17 | python | 功能 | | :----------------------------------------- | :------------------- | :---------------------------------- | | filesystem::path::is_absolute() | os.path.isabs() | 判断是否为绝对路径 | | filesystem::path::parent_path() | os.path.dirname() | 路径分割 | | filesystem::path::filename() | os.path.basename() | 路径分割 | | filesystem::operator/() | os.path.join() | 路径拼接 | | filesystem::current_path() | os.getcwd() | 获取当前路径 | | filesystem::directory_iterator | os.listdir() | 返回指定目录下的所有文件/文件夹 | | filesystem::recursive_directory_iterator | os.walk() | 递归返回指定目录下的所有文件/文件夹 | | filesystem::exists() | os.path.exists() | 判断路径是否存在 | | filesystem::is_regular_file() | os.path.isfile() | 判断路径是文件还是目录 | | filesystem::is_directory() | os.path.isdir() | 判断路径是文件还是目录 | | filesystem::absolute() | os.path.abspath() | 返回绝对路径 | | filesystem::copy_file() | shutil.copyfile() | 文件拷贝 | | filesystem::remove() | os.remove() | 文件删除 | | filesystem::copy() | shutil.copytree | 路径拷贝 | | filesystem::remove_all | shutil.rmtree() | 路径删除 |filesystem::path vs. os.pathfilesystem::path是一个类,里面封装了很多方法,我们通过实例化之后直接调用方法。os.path是一个模块,里面有很多函数,可以直接调用。判断是否为绝对路径 什么是绝对路径?我个人的理解是从根目录开始的就是绝对路径,例如/usr/local和C:\\Users,其余都是相对路径。可以发现,在不同操作系统中路径的分割符是不同的。同时在相对路径中./和../有特殊含义,./表示当前目录,../表示上一层目录,相应地,../../就是上两层目录。 1.filesystem::path中提供了判断是否为绝对路径/相对路径方法。 _LIBCPP_INLINE_VISIBILITY bool is_absolute() const { return has_root_directory(); } _LIBCPP_INLINE_VISIBILITY bool is_relative() const { return !is_absolute(); } 可以发现,判断相对路径的结果就是绝对路径取反。void eg1_1() { /*判断是否为绝对路径*/ // std::filesystem::path abs_path = "C:\\Users"; std::filesystem::path abs_path = "/usr/local"; // 注意,实例化path的时候可以直接用等号 std::cout << "abs_path.is_absolute() : " << abs_path.is_absolute() << std::endl; std::cout << "abs_path.is_relative() : " << abs_path.is_relative() << std::endl; std::filesystem::path rel_path = "../"; std::cout << "rel_path.is_absolute() : " << rel_path.is_absolute() << std::endl; std::cout << "rel_…
正在初始化 WebAssembly 引擎…
首次编译原生模块可能需要数秒
就绪后,页面交互将以接近原生的速度运行