当前位置:首页 » 编程语言 » python查看文件存在

python查看文件存在

发布时间: 2022-08-20 09:06:04

A. 我如何检查文件是否存在使用python

使用 os 模块。

importos

dir="你的文件或者文件夹目录"

#一种常用的判断文件目录不存在并创建目录的方法
#当然如果只是判断是否存在,同样适用于文件
ifnotos.path.exists(dis_dir):
os.mkdir(dis_dir)

B. 如何用Python判断文件是否存在

可以使用 os 模块的 os.path.exists()
比如下面 我的path1路径下的文件是存在的,path2路径下的文件不存在。
In [1]: path1 = r'E:\result\1.jpg'
In [2]: path2 = r'E;\result\100.jpg'
In [3]: import os
In [4]: os.path.exists(path1)
Out[4]: True
In [5]: os.path.exists(path2)
Out[5]: False

C. Python3如何检查文件是否存在

importos
deffilecheck(path):
ifos.path.exists(path):
print("%sisexist"%path)

D. python判断文件内是否存在某字符串

方法:使用 in 方法实现contains的功能:

1 site = 'http://www.jb51.net/'

2 if "jb51" in site:

3 print('site contains jb51')

输出结果:site contains jb51

E. python 如何判断一个文件是否存在

os.path.exists(),如果文件存在返回True
例如:
import os
print(os.path.exists('123.txt')

F. 如何用Python实现查找"/"目录下的文件夹或文件,感谢

给你各相对来说容易理解的哈
import os
name=raw_input('filename:') #在这里输入你的查找值
a=os.listdir('/') #把所有/目录下的文件,目录存放入a
if name in a: #如果查找值在/目录下,进行进一步判断
if os.path.isdir(name): #判断是否为目录

print 'dir'

elif os.path.isfile(name) and os.pathislink(name): #符号连接即是文件又是link所以双重判断

print 'link'

elif os.path.isfile(name): #判断是否文件

print 'file'

else: #linux上文件类型多,不符合上面三种打印0ther

print 'other'
else: #不存在打印‘not exist’
print 'not exist'

G. 如何检查文件是否存在于远程服务器上

在有些情况下,你要测试文件是否存在于远程Linux服务器的某个目录下(例如:/var/run/test_daemon.pid),而无需登录到远程服务器进行交互。例如,你可能希望你的脚本根据特定文件是否存在的远程服务器上而由不同的行为。
在本教程中,我将向您展示如何使用不同的脚本语言(如:Bash shell,Perl,Python)查看远程文件是否存在。
这里描述的方法将使用ssh访问远程主机。您首先需要启用无密码的ssh登录到远程主机,这样您的脚本可以在非交互式的批处理模式访问远程主机。您还需要确保ssh登录文件有读权限检查。假设你已经完成了这两个步骤,您可以编写脚本就像下面的例子
使用bash判断文件是否存在于远程服务器上
#!/bin/bash

ssh_host="xmolo@remote_server"
file="/var/run/test.pid"

if ssh $ssh_host test -e $file;
then echo $file exists
else echo $file does not exist
fi

使用perl判断文件是否存在于远程服务器上
#!/usr/bin/perl

my $ssh_host = "xmolo@remote_server";
my $file = "/var/run/test.pid";

system "ssh", $ssh_host, "test", "-e", $file;
my $rc = $? >> 8;
if ($rc) {
print "$file doesn't exist\n";
} else {
print "$file exists\n";
}

使用python判断文件是否存在于远程服务器上
#!/usr/bin/python

import subprocess
import pipes

ssh_host = 'xmolo@remote_server'
file = '/var/run/test.pid'

resp = subprocess.call(
['ssh', ssh_host, 'test -e ' + pipes.quote(file)])

if resp == 0:
print ('%s exists' % file)
else:
print ('%s does not exist' % file)

H. python如何判断一个目录下是否存在某个文件

1.使用os模块

  • 用os模块中os.path.exists()方法检测是否存在test_file.txt文件

importos
os.path.exists(test_file.txt)
#True
os.path.exists(no_exist_file.txt)
#False

2.使用Try命令

  • 使用open()方法,如果要打开的文件不存在,就回跑出异常,用try()方法捕获异常。

try:
f=open(test_file.txt)
f.close()
exceptIOError:
print"fileisnotaccessible"

3. 使用pathlib

  • 检查路径是否存在

path=pathlib.Path("path/file")
path.exist()
  • 检查路径是否是文件

path=pathlib.Path("path/file")
path.is_file()

I. python3 不使用os 模块检查文件是否存在

以读模式打开,看看能否打开成功就可以了。只需要open函数。

热点内容
莱克发的工资卡密码是多少 发布:2025-05-14 16:57:10 浏览:177
方舟怎么用自己的存档进入别人的服务器 发布:2025-05-14 16:46:25 浏览:876
微博视频高清上传设置 发布:2025-05-14 16:38:41 浏览:548
数据库图书管理设计 发布:2025-05-14 16:33:52 浏览:378
php开发的网页 发布:2025-05-14 16:22:03 浏览:477
服务器内存跑满了怎么回事 发布:2025-05-14 16:21:16 浏览:224
微信qq音乐缓存 发布:2025-05-14 16:16:16 浏览:469
c语言回收内存 发布:2025-05-14 16:16:08 浏览:144
2021国产安卓顶级旗舰买哪个 发布:2025-05-14 16:15:36 浏览:300
linux自学视频 发布:2025-05-14 16:14:49 浏览:256