当前位置:首页 » 文件管理 » 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自学网!!

热点内容
安卓和csharp哪个发展好 发布:2024-05-18 18:09:30 浏览:526
换编程题库 发布:2024-05-18 18:00:58 浏览:562
如何使用服务器ip直连网站 发布:2024-05-18 18:00:49 浏览:432
三星n7100哪个安卓版本好用 发布:2024-05-18 17:55:41 浏览:489
万国觉醒采集脚本源码 发布:2024-05-18 17:55:39 浏览:947
sqlserver加字段 发布:2024-05-18 17:54:53 浏览:928
安卓手机如何清除应用记录 发布:2024-05-18 17:31:37 浏览:639
查看存储过程权限 发布:2024-05-18 17:18:33 浏览:192
php类self 发布:2024-05-18 17:15:03 浏览:895
手机2b2t的服务器地址是多少 发布:2024-05-18 17:14:56 浏览:189