-
Python 如何测试文件是否存在
2020-09-16 10:45:34主要介绍了Python 如何测试文件是否存在,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下 -
Python 测试文件是否存在
2019-10-18 14:46:56测试文件是否存在 问题 你想测试一个文件或目录是否存在。 解决方案 使用os.path模块来测试一个文件或目录是否存在。比如: >>> import os >>> os.path.exists('/etc/passwd') True >>...测试文件是否存在
问题
你想测试一个文件或目录是否存在。
解决方案
使用
os.path
模块来测试一个文件或目录是否存在。比如:>>> import os >>> os.path.exists('/etc/passwd') True >>> os.path.exists('/tmp/spam') False >>>
你还能进一步测试这个文件时什么类型的。 在下面这些测试中,如果测试的文件不存在的时候,结果都会返回False:
>>> # Is a regular file >>> os.path.isfile('/etc/passwd') True >>> # Is a directory >>> os.path.isdir('/etc/passwd') False >>> # Is a symbolic link >>> os.path.islink('/usr/local/bin/python3') True >>> # Get the file linked to >>> os.path.realpath('/usr/local/bin/python3') '/usr/local/bin/python3.3' >>>
如果你还想获取元数据(比如文件大小或者是修改日期),也可以使用
os.path
模块来解决:>>> os.path.getsize('/etc/passwd') 3669 >>> os.path.getmtime('/etc/passwd') 1272478234.0 >>> import time >>> time.ctime(os.path.getmtime('/etc/passwd')) 'Wed Apr 28 13:10:34 2010' >>>
讨论
使用
os.path
来进行文件测试是很简单的。 在写这些脚本时,可能唯一需要注意的就是你需要考虑文件权限的问题,特别是在获取元数据时候。比如:>>> os.path.getsize('/Users/guido/Desktop/foo.txt') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.3/genericpath.py", line 49, in getsize return os.stat(filename).st_size PermissionError: [Errno 13] Permission denied: '/Users/guido/Desktop/foo.txt' >>>
-
《Python Cookbook 3rd》笔记(5.12):测试文件是否存在
2020-12-03 11:05:01测试文件是否存在 问题 你想测试一个文件或目录是否存在。 解法 使用 os.path 模块来测试一个文件或目录是否存在。比如: >>> import os >>> os.path.exists('/etc/passwd') True >>> os...测试文件是否存在
问题
你想测试一个文件或目录是否存在。
解法
使用 os.path 模块来测试一个文件或目录是否存在。比如:
>>> import os >>> os.path.exists('/etc/passwd') True >>> os.path.exists('/tmp/spam') False >>>
你还能进一步测试这个文件时什么类型的。在下面这些测试中,如果测试的文件不存在的时候,结果都会返回 False:
>>> # Is a regular file >>> os.path.isfile('/etc/passwd') True >>> # Is a directory >>> os.path.isdir('/etc/passwd') False >>> # Is a symbolic link >>> os.path.islink('/usr/local/bin/python3') True >>> # Get the file linked to >>> os.path.realpath('/usr/local/bin/python3') '/usr/local/bin/python3.3' >>>
如果你还想获取元数据 (比如文件大小或者是修改日期),也可以使用 os.path 模块来解决:
>>> os.path.getsize('/etc/passwd') 3669 >>> os.path.getmtime('/etc/passwd') 1272478234.0 >>> import time >>> time.ctime(os.path.getmtime('/etc/passwd')) 'Wed Apr 28 13:10:34 2010' >>>
讨论
使用 os.path 来进行文件测试是很简单的。在写这些脚本时,可能唯一需要注意的就是你需要考虑文件权限的问题,特别是在获取元数据时候。比如:
>>> os.path.getsize('/Users/guido/Desktop/foo.txt') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.3/genericpath.py", line 49, in getsize return os.stat(filename).st_size PermissionError: [Errno 13] Permission denied: '/Users/guido/Desktop/foo.txt' >>>
-
python怎么检测文件_Python 如何测试文件是否存在
2020-12-08 13:44:44你想测试一个文件或目录是否存在。解决方案使用 os.path 模块来测试一个文件或目录是否存在。比如:>>> import os>>> os.path.exists('/etc/passwd')True>>> os.path.exists('/tmp/spam'...你想测试一个文件或目录是否存在。
解决方案
使用 os.path 模块来测试一个文件或目录是否存在。比如:
>>> import os
>>> os.path.exists('/etc/passwd')
True
>>> os.path.exists('/tmp/spam')
False
>>>
你还能进一步测试这个文件时什么类型的。 在下面这些测试中,如果测试的文件不存在的时候,结果都会返回False:
>>> # Is a regular file
>>> os.path.isfile('/etc/passwd')
True
>>> # Is a directory
>>> os.path.isdir('/etc/passwd')
False
>>> # Is a symbolic link
>>> os.path.islink('/usr/local/bin/python3')
True
>>> # Get the file linked to
>>> os.path.realpath('/usr/local/bin/python3')
'/usr/local/bin/python3.3'
>>>
如果你还想获取元数据(比如文件大小或者是修改日期),也可以使用 os.path 模块来解决:
>>> os.path.getsize('/etc/passwd')
3669
>>> os.path.getmtime('/etc/passwd')
1272478234.0
>>> import time
>>> time.ctime(os.path.getmtime('/etc/passwd'))
'Wed Apr 28 13:10:34 2010'
>>>
讨论
使用 os.path 来进行文件测试是很简单的。 在写这些脚本时,可能唯一需要注意的就是你需要考虑文件权限的问题,特别是在获取元数据时候。比如:
>>> os.path.getsize('/Users/guido/Desktop/foo.txt')
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.3/genericpath.py", line 49, in getsize
return os.stat(filename).st_size
PermissionError: [Errno 13] Permission denied: '/Users/guido/Desktop/foo.txt'
>>>
以上就是Python 如何测试文件是否存在的详细内容,更多关于Python 测试文件的资料请关注其它相关文章!
-
shell脚本测试变量是否为空,测试文件是否存在,sed修改配置文件参数,分支语句
2020-04-20 16:35:30Shell脚本 1. 基本的几个变量 使用$?获取最近一次的执行结果;...场景:在某个目录查找是否包含某个文件 一般使用双引号("")将变量括起来。将多个变量化为一个字符串,否则测试时可能会有warning ###########...Shell脚本
1. 基本的几个变量
- 使用$?获取最近一次的执行结果;
- 使用$#获取传递的参数个数,类似C语言中的int argc;
- 使用$@获取所有的传参,类似C语言的char **argv
2. 获取一个命令的结果是否为空
场景:在某个目录查找是否包含某个文件
一般使用双引号("")将变量括起来。将多个变量化为一个字符串,否则测试时可能会有warning
######################################################################### # File Name: common_usages.sh # Author: Toney Sun # mail: vip_13031075266@163.com # Created Time: 2020年04月20日 星期一 15时03分23秒 ######################################################################### #!/bin/bash <<AAA 获取一个命令的结果是否为空 例如: 在某个目录查找是否包含某个文件 一般使用双引号("")将变量括起来。将多个变量化为一个字符串,否则测试时可能会有warning AAA isNull(){ local result DIR="/mnt/hgfs/em嵌入式学习记录/shell/" #result=`ls | grep shell.txt` result=`ls ../` #echo "${result}" #./common_uages.sh: line 21: [: too many arguments if [ -z "$result" ]; then echo "Can't find shell.txt in $PWD !!!" else echo "shell.txt is in current path." fi #上述命令可以简化为下面的方式: [ -z "$result" ] && echo "Can't find shell.txt" || echo "shell.txt is in current path." }
3. 检测文件系统中是否存在某个文件
场景: 需要解压缩一个tar包,首先得确认tar包存在;
######################################################################### # File Name: common_usages.sh # Author: Toney Sun # mail: vip_13031075266@163.com # Created Time: 2020年04月20日 星期一 15时03分23秒 ######################################################################### #!/bin/bash <<BBB 检测文件系统中是否存在某个文件 例如: 需要解压缩一个tar包,首先得确认tar包存在; BBB isFileExist(){ FILE="linux-2.6.24.tar.bz2" [ -e $FILE ] && echo "$FILE is exist." || echo "$FILE is not exist!!!" DST_PATH="/tmp/linux2.6.24" #如果想要解压到”/tmp/linux-2.6.24目录“,那么我们首先需要检测该目录是否存在 #如果存在,删除该目录下的所有内容;如果不存在则创建该目录 [ ! -e ${DST_PATH} ] && echo "${DST_PATH} not exist..." [ -e ${DST_PATH} ] && rm -f ${DST_PATH}/* || mkdir ${DST_PATH} }
3. 使用sed命令修改一整行内容
场景: 修改配置文件的值
######################################################################### # File Name: common_usages.sh # Author: Toney Sun # mail: vip_13031075266@163.com # Created Time: 2020年04月20日 星期一 15时03分23秒 ######################################################################### #!/bin/bash <<CCC 使用sed命令修改一整行内容 例如: 修改配置文件的值 CCC modifyConfig(){ FILE="common_usage.log" version="linux-4.1.23" #正常的做法-->不可行 #以”REVISION=“开始的行全部替换为”REVISION=$version“ #sed -i 's/REVISION=.*$/REVISION=$version/' $FILE #可行办法 sed -i "s/REVISION=.*$/REVISION=$version/" $FILE }
3. 检测文件系统中是否存在某个文件
场景: 需要解压缩一个tar包,首先得确认tar包存在;
######################################################################### # File Name: common_usages.sh # Author: Toney Sun # mail: vip_13031075266@163.com # Created Time: 2020年04月20日 星期一 15时03分23秒 ######################################################################### #!/bin/bash <<BBB 检测文件系统中是否存在某个文件 例如: 需要解压缩一个tar包,首先得确认tar包存在; BBB isFileExist(){ FILE="linux-2.6.24.tar.bz2" [ -e $FILE ] && echo "$FILE is exist." || echo "$FILE is not exist!!!" DST_PATH="/tmp/linux2.6.24" #如果想要解压到”/tmp/linux-2.6.24目录“,那么我们首先需要检测该目录是否存在 #如果存在,删除该目录下的所有内容;如果不存在则创建该目录 [ ! -e ${DST_PATH} ] && echo "${DST_PATH} not exist..." [ -e ${DST_PATH} ] && rm -f ${DST_PATH}/* || mkdir ${DST_PATH} }
4. shell脚本中的分支语句
场景: 根据不同的参数执行不同的操作;获取函数返回值
######################################################################### # File Name: common_usages.sh # Author: Toney Sun # mail: vip_13031075266@163.com # Created Time: 2020年04月20日 星期一 15时03分23秒 ######################################################################### #!/bin/bash condition(){ if [ $# -eq 1 ]; then case "$1" in clean) echo "make clean..." return 0 ;; all.clean) echo "make all.clean..." return 0 ;; *) echo "make default..." return 1 ;; esac elif [ $# -eq 2 ]; then echo "Two parameters...." return 1 else cat <<-USAGE Usage: $0 [clean|clean.all] [para1 para2] USAGE fi } condition asdf [ $? -eq 0 ] && echo "return sucess!!!" || echo "failed!!!"
-
linux测试文件是否存在命令集合
2016-05-29 22:07:16-b FILE FILE exists and is block special -c FILE FILE exists and is character special -d FILE FILE exists and is a directory -e FILE -
shell步步进阶---测试文件是否存在的2种shell写法
2015-07-21 18:07:32在Linux中写脚本的时候,总免不了需要判断文件是否存在、文件内容是否为空等存在,而这些操作都可以用test 指令来实现,通过 man test 指令可以查看关于test指令的手册,手册中有如下说明: -s FILE FILE... -
服务器访问或下载oss文件---测试文件是否存在或下载速度
2019-09-05 17:47:01参考链接:...下载文件:wget ‘filepath’ 注意filepath需要使用’'包起来, 否则容易产生下载失败403错误, 原因是字符问题 filepath的来源oss文件url... -
显示文件夹中的所有图像,而不测试文件是否存在
2012-03-27 04:22:29<p>Using the following code, I am able to display up to 10 images. What I want is to be able to display all images in the folder without going through an infinite loop and without testing if the ... -
golang 文件是否存在_如何测试Go中是否存在文件或目录?
2020-07-28 10:13:48golang 文件是否存在How to test a path (a file or directory exists) in golang? 如何在golang中测试路径(文件或目录存在)? In Bash, the [ -e ] tests can test whether a file or a directory exist. ... -
检查文件夹/文件是否存在
2019-06-20 17:24:00测试文件是否存在 解决方案 使用 os.path 模块来测试一个文件或目录是否存在。比如: import os print(os.path.exists('/Users/xz/test')) True print(os.path.exists('/Users/xz/test/cookbook')) ... -
python 测试文件或者文件目录是否存在 测试文件类型,获取文件大小,获取修改日期...
2019-01-07 11:10:00----测试一个文件或目录是否存在 >>> import os >>> os.path.exists('/etc/passwd') True >>> os.path.exists('/tmp/spam') False >>> ----测试这个文件时什么... -
Linux中判断文件是否存在
2013-03-27 11:01:35用access函数 access的第一个参数是文件路径,第二个参数是测试的模式。常用的有R_OK:测试读权限,W_OK:测试写权限,X_OK:测试执行权限,F_OK:测试文件是否存在; -
检查文件是否存在于远程服务器上
2017-05-16 11:45:23在有些情况下,你要测试文件是否存在于远程Linux服务器的某个目录下(例如:/var/run/test_daemon.pid),而无需登录到远程服务器进行交互。例如,你可能希望你的脚本根据特定文件是否存在的远程服务器上而由不同的... -
如何测试Python中是否存在文件或目录?
2020-07-21 03:59:57In Bash, the [ -f ] and [ -d ] tests can test whether a file or a directory exist. What are the ... 在Bash中, [ -f ]和[ -d ]测试可以测试文件或目录是否存在。 这些测试对应的python方法是什么? ... -
如何使用Bash测试文件中是否存在字符串?
2020-07-11 15:44:53I have a file that contains directory names: 我有一个包含目录名称的文件: my_list.txt : my_list.txt : /tmp /va -
linux判断文件,目录是否存在
2019-10-12 11:58:17F_OK 测试文件是否存在 R_OK 测试读权限 W_OK 测试写权限 X_OK 测试执行权 2 目录存在 opendir函数用来打开文件目录,成功返回指针,出错返回NULL #include <iostream> #include <unistd.h>... -
测试某个目录中文件是否存在
2011-08-05 12:07:42BOOL COJServerDlg::FileExit(char* szfile) { string fileName = szfile; CString appPath = GetAppPath(); string seperator = "\\";...string fullPath = appPath.GetBuffer(0) +seperator+fileName;... -
shell脚本判断文件是否存在_shell脚本编程学习之路-文件测试表达式
2020-12-08 15:49:571.文件测试表达式的用法我们在编程时处理一个对象时,需要对对象进行测试,只有符合要求的才采取操作处理;这样做的好处是避免程序出错以及无所畏的消耗系统资源,这个测试的对象可以是文件、字符串、数字等。下表为... -
测试文件是否可读、存在等
2011-11-30 10:14:001 #include <unistd.h> 2 #include <sys/stat.h> 3 #include <stdio.h> 4 5 bool IsFileRead(const char * file_name) 6 { 7 int ret = access(file_name, R_OK); 8 /* 9 W_... -
python查找文件是否存在_python检查指定文件是否存在的方法
2020-12-01 18:59:47{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云文件存储NAS是一个可共享访问,弹性扩展,高可靠,高性能的分布式文件系统。... -
java 判断文件是否存在 按行读取 自己的修改版 测试通过
2018-08-02 18:25:45首先,这样的代码这十多年写过不少次了,但好像每次写的都不太一样。以前一直都流行按字节读取。每次都从网上找来差不多的,然后用。但网上有不少按行读取再写出的代码,但我在使用之后发现读取时总会有些莫名其妙的... -
使用Bash Shell检查文件是否存在的方法
2020-09-15 12:18:57大多数情况下,可以使用测试命令来对条件进行...比如可以比较字符串、判断文件是否存在及是否可读等等。下面这篇文章就主要介绍了使用Bash Shell检查文件是否存在的方法,需要的朋友可以参考借鉴,下面来一起看看吧。 -
Linux检查文件是否存在
2018-04-29 02:10:062. 用bash shell检查假设我们有文件目录/home/benben/go_project和文件/home/benben/go_project/test.txt,下面我们用bash shell命令来检测下这个目录和文件是否存在。 检查目录,执行命令[-d /home/benben/go_... -
c# 写文件时 目录不存在_如何测试C ++中是否存在文件或目录?
2020-07-28 07:31:44c# 写文件时 目录不存在 How to test a path (a ... 如何在C ++中测试路径(文件或目录存在)? In Bash, the [ -f ] and [ -d ] tests can test whether a file or a directory exist. What are the corre... -
c语言 判断文件是否存在
2021-01-15 17:13:32检查调用进程是否可以对指定的文件执行某种操作。 用法: #include <unistd.h> #include <fcntl.h> int access(const char *pathname, int mode); pathname: 需要测试的文件路径名。 mode: 需要... -
判断文件是否存在
2019-09-20 18:08:49参考博客:理解bash的if语句 if语法:和大多语言差不多,condition为'true'就yes,不然就no ifcondition;then yes else no fi ...首先创建一个测试文件existed.txt,建立shell脚本testFile.sh ... -
检查测试环境是否存在本番数据
2018-02-01 22:19:24检查测试环境是否存在本番信息 检查测试WS,是否存在本番设置 # 1.检索项目下的xml、json、properties配置文件 # -regex '.*\.xml|.*\.json|.*\.properties' # 2.检索文件的'10.2.、192.168.、@' # -E '10\.2\.|...
-
关于Redis命令使用和说明
-
Kotlin协程极简入门与解密
-
进程调度:介绍
-
netflix ribbon 之 LoadBalancerRule
-
重装系统后老系统的回收站如何删除
-
【数据分析-随到随学】Python数据获取
-
数据类型转换、运算符、方法入门
-
Excel高级图表技巧
-
欧美化妆品网站模板
-
MySQL笔记之JDBC
-
【LeetCode力扣】给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。
-
(一)安装最新的 Visual Studio ,开启ML.NET (机器学习) 预览功能
-
Swoole扩展安装教程
-
深入.net平台
-
女性饰品网上商城网页模板
-
2021-1-21-卡尔曼滤波
-
婚礼用品网页模板
-
电设20190807074129420.rar
-
恐龙世界响应式网页模板
-
最新最全论文合集——北京大学-大数据科学研究中心