当前位置:首页 > 编程技术 > 正文

如何检测文件是否存在

如何检测文件是否存在

在编程中,检测文件是否存在通常可以通过以下几种方法实现: 1. Python```pythonimport osfile_path = 'example.txt'if...

在编程中,检测文件是否存在通常可以通过以下几种方法实现:

1. Python

```python

import os

file_path = 'example.txt'

if os.path.exists(file_path):

print("文件存在")

else:

print("文件不存在")

```

2. Java

```java

import java.io.File;

File file = new File("example.txt");

if (file.exists()) {

System.out.println("文件存在");

最新文章