當前位置:首頁 » 文件管理 » python文件夾是否存在

python文件夾是否存在

發布時間: 2022-11-14 21:21:45

⑴ R、python文件夾操作

Python OS模塊

1.重命名:os.rename(old, new)
2.刪除:os.remove(file)
3.列出目錄下的 文件 :os.listdir(path)
4.獲取當前工作目錄:os.getcwd()
5.改變工作目錄:os.chdir(newdir)
6.創建多級目錄:os.makedirs(r"c:/python /test")
7.創建單個目錄:os.mkdir("test")
8.刪除多個目錄:os.removedirs(r"c:/python") #刪除所給路徑最後一個目錄下所有空目錄。
9.刪除單個目錄:os.rmdir("test")
10.獲取文件屬性:os.stat(file)
11.修改文件許可權與時間戳:os.chmod(file)
12.執行操作系統 命令:os.system("dir")
13.啟動新進程:os.exec(), os.execvp()
14.在後台執行程序:osspawnv()
15.終止當前進程:os.exit(), os._exit()
16.分離文件名:os.path.split(r"c:/python/ hello.py ") –> ("c://python", " hello.py ")
17.分離擴展名:os.path.splitext(r"c:/python/ hello.py ") –> ("c://python//hello", ".py")
18.獲取路徑名:os.path.dirname(r"c:/python/ hello.py ") –> "c://python"
19.獲取文件名:os.path.basename(r"r:/python/hello.py") –> "hello.py"
20.判斷文件是否存在:os.path.exists(r"c:/python/hello.py") –> True
21.判斷是否是絕對路徑:os.path.isabs(r"./python/") –> False
22.判斷是否是目錄:os.path.isdir(r"c:/python") –> True
23.判斷是否是文件:os.path.isfile(r"c:/python/hello.py") –> True
24.判斷是否是鏈接文件:os.path.islink(r"c:/python/hello.py") –> False
25.獲取文件大小:os.path.getsize(filename)
26.*******:os.ismount("c://") –> True
27.搜索目錄下的所有文件:os.path.walk()
[2.shutil]
1.復制單個文件:shultil.(oldfile, newfle)
2.復制整個目錄樹:shultil.tree(r"./setup", r"./backup")
3.刪除整個目錄樹:shultil.rmtree(r"./backup")
[3.tempfile]
1.創建一個唯一的臨時文件:tempfile.mktemp() –> filename
2.打開臨時文件:tempfile.TemporaryFile()
[4.StringIO] #cStringIO是StringIO模塊的快速實現模塊
1.創建內存 文件並寫入初始數據 :f = StringIO.StringIO("Hello world!")
2.讀入內存文件數據:print f.read() #或print f.getvalue() –> Hello world!
3.想內存文件寫入數據:f.write("Good day!")
4.關閉內存文件:f.close()

rm(list=ls())
path = 'J:/lab/EX29 --在R語言中進行文件(夾)操作'
setwd(path)
cat("file A\n", file="A") #創建一個文件A,文件內容是'file A','\n'表示換行,這是一個很好的習慣
cat("file B\n", file="B") #創建一個文件B
file.append("A", "B") #將文件B的內容附到A內容的後面,注意沒有空行
file.create("A") #創建一個文件A, 注意會覆蓋原來的文件
file.append("A", rep("B", 10)) #將文件B的內容復制10便,並先後附到文件A內容後
file.show("A") #新開工作窗口顯示文件A的內容
file.("A", "C") #復制文件A保存為C文件,同一個文件夾
dir.create("tmp") #創建名為tmp的文件夾
file.(c("A", "B"), "tmp") #將文件夾拷貝到tmp文件夾中
list.files("tmp") #查看文件夾tmp中的文件名
unlink("tmp", recursive=F) #如果文件夾tmp為空,刪除文件夾tmp
unlink("tmp", recursive=TRUE) #刪除文件夾tmp,如果其中有文件一並刪除
file.remove("A", "B", "C") #移除三個文件

⑵ Python使用判斷,檢查是都存在1.TXT文件,如果不存在,返迴文字不存在!怎麼寫這段代碼

檢查文件是否存在的方法,在Python3文件操作中經常被用到,因為,只有文件存在,我們才可以對文件進行下一步處理,那麼,常用的檢查文件存在的方法有哪些呢?以下是Python3檢查文件是否存在的幾種方法。
一、 使用os庫
os庫方法可檢查文件是否存在,存在返回Ture,不存在返回False,且不需要打開文件。
1. os.path.isfile文件檢查
import os.path
filename='/oldboye.com/file.txt'
os.path.isfile(filename)
2. os.path.exists文件夾檢查
import os
a_path='/oldboye.com/'
if os.path.exists(a_path):
#do something
3. os.access文件許可權檢查
import os
filename='/oldboye.com/file.txt'
if os.path.isfile(filename) and os.access(filename, os.R_OK):
#do something
二、使用pathlib庫
使用pathlib庫也是一種檢查文件是否存在的方法,且從Python3.4開始,Python已經把pathlib加入了標准庫,無需安裝,即可直接使用!
1. 檢查文件是否存在
from pathlib import Path
my_file = Path("/oldboye.com/file.txt")
if my_file.is_file():
# file exists
2. 檢查文件夾是否存在
from pathlib import Path
my_file = Path("/oldboye.com/file.txt")
if my_file.is_dir():
# directory exists
3. 文件或文件夾是否存在
from pathlib import Path
my_file = Path("/oldboye.com/file.txt")
if my_file.exists():
# path exists
以上列舉Python3中檢查文件和文件夾的兩種常用的方法,適用於Python3相關版本,其他版本略有不同,可以根據實際情況進行設置!

⑶ python 如何判斷文件夾為空文件夾求可執行代碼

1、def del_file_items(spath):

import os

paths = os.listdir(spath)

for pa in paths:

filepath = os.path.join(spath,pa)

if os.path.isfile(filepath):

try:

2、os.remove(filepath)

except os.error:

print "remove %s error." %filePath

elif os.path.isdir(filepath):

try:

3、##在方法內可以引用自身

del_file_items(filepath)

except os.error:

print "remove %s

⑷ arcgispython怎麼判斷文件夾是否存在某個文件

您好,你的問題,我之前好像也遇到過,以下是我原來的解決思路和方法,希望能幫助到你,若有錯誤,還望見諒!使用os.path.exists()方法可以直接判斷文件是否存在。
代碼如下:
>>> import os
>>> os.path.exists(r'C:\1.TXT')
False
>>>
如果存在返回值為True如果不存在則返回False。很方便
希望對你有所幫助~~非常感謝您的耐心觀看,如有幫助請採納,祝生活愉快!謝謝!

⑸ python 怎麼判斷遠程伺服器的文件是否存在

importos
ifos.path.exists('path'):
dosth
else:
dosth

⑹ python 看是否存在文件夾 Python 判斷文件/目錄是否存在

1、Python 操作文件時,我們一般要先判斷指定的文件或目錄是否存在,不然容易產生異常。

2、例如我們可以使用 os 模塊的 os.path.exists() 方法來檢測文件是否存在:

import os.path

os.path.isfile(fname)

3、如果你要確定他是文件還是目錄,從 Python 3.4 開始可以使用 pathlib 模塊提供的面向對象的方法 (Python 2.7 為 pathlib2 模塊):

from pathlib import Path

my_file = Path(/path/to/file)

if my_file.is_file():

# 指定的文件存在

檢測是否為一個目錄:

if my_file.is_dir():

# 指定的目錄存在

4、如果要檢測路徑是一個文件或目錄可以使用 exists() 方法:

if my_file.exists():

# 指定的文件或目錄存在

在 try 語句塊中你可以使用 resolve() 方法來判斷:

try:

my_abs_path = my_file.resolve()

except FileNotFoundError:

# 不存在

else:

# 存在

⑺ 怎樣在python中判斷一個文件是否存在

你可以用os.path.isfile

如果路徑下是現有普通文件返回true。因此islink()和isflie()都可以來判斷相同目錄下是否有文件。

import os.path
os.path.isfile(fname)

在Python3.4之後pathlib模塊提供了一種面向對象的方法用於判斷文件是否存在:

from pathlib import Path

my_file = Path("/path/to/file")
if my_file.is_file():
# file exists

⑻ 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()

⑼ 文件是存在的 為什麼python提示文件不存在

一、python判斷文件和文件夾是否存在、創建文件夾

代碼如下:

>>> import os
>>>
os.path.exists('d:/assist')
True
>>>
os.path.exists('d:/assist/getTeacherList.py')
True
>>>
os.path.isfile('d:/assist')
False
>>>
os.path.isfile('d:/assist/getTeacherList.py')
True
>>>
os.makedirs('d:/assist/set')
>>>
os.path.exists('d:/assist/set')
True

⑽ python如何用if判斷文件夾是否存在

python用if判斷文件夾是否存在的方法:

python的os模塊可以對文件夾進行操作。使用if語句「os.path.exists()」函數的返回值是否是True,如果是則輸出該文件夾存在

示例:判斷文件kk是否存在

代碼如下:

執行結果如下:

更多Python知識,請關註:Python自學網!!

熱點內容
我的世界模組伺服器推薦手機版 發布:2024-05-05 05:02:49 瀏覽:817
pr默認存儲 發布:2024-05-05 04:29:31 瀏覽:553
roblox跑酷腳本怎麼做 發布:2024-05-05 03:57:35 瀏覽:702
捷徑清理緩存 發布:2024-05-05 03:57:35 瀏覽:479
ftputility哪裡下載 發布:2024-05-05 03:47:13 瀏覽:1001
雷凌運動版如何連接安卓手機導航 發布:2024-05-05 03:42:48 瀏覽:268
自動鬼使黑腳本 發布:2024-05-05 03:10:49 瀏覽:880
游戲腳本編程書籍推薦 發布:2024-05-05 02:59:13 瀏覽:72
編譯器書籍推薦 發布:2024-05-05 02:57:02 瀏覽:56
電池存儲溫度 發布:2024-05-05 02:53:07 瀏覽:207