當前位置:首頁 » 編程語言 » 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函數。

熱點內容
sqlserver建立視圖 發布:2025-05-14 13:11:56 瀏覽:483
搭建httpsgit伺服器搭建 發布:2025-05-14 13:09:47 瀏覽:255
新電腦拿回來我該怎麼配置 發布:2025-05-14 13:09:45 瀏覽:240
視頻伺服器新建ftp用戶 發布:2025-05-14 13:03:09 瀏覽:225
php花生 發布:2025-05-14 12:54:30 瀏覽:550
java人才 發布:2025-05-14 12:29:10 瀏覽:649
如何打開軟密碼 發布:2025-05-14 12:28:55 瀏覽:427
七牛存儲待遇 發布:2025-05-14 12:27:20 瀏覽:422
C語言a35a4a5 發布:2025-05-14 11:53:48 瀏覽:814
android隱藏item 發布:2025-05-14 11:43:56 瀏覽:328