英文:
How do I find multple files with file extension *.nmconnection in Python?
问题
我尝试查找/etc/NetworkManager/system-connections/
目录中是否存在单个文件或文件扩展名为*.nmconnection
的文件。
我尝试了许多方法,比如os.path.isdir
,但是没有解决这个问题。
我的目标是,如果存在带有*.nmconnection
文件扩展名的文件或单个文件,则返回True
,否则返回False
。
import glob, os
os.chdir("/etc/NetworkManager/system-connections/")
for file in glob.glob("*.nmconnection"):
print(file)
解决我的问题的示例。
英文:
I try to find if there are a single file or files with file extension *.nmconnection
in /etc/NetworkManager/system-connections/
I try a lots of methods like os.path.isdir
but I didn't solve it.
My goal is, if there are files or single file with *.nmconnection
file extension then I get True
or If not I will get False
import glob, os
os.chdir("/etc/NetworkManager/system-connections/")
for file in glob.glob("*.nmconnection"):
print(file)
Examples to solve my problem.
答案1
得分: 0
使用pathlib
的Path
和glob
功能,您可以这样做:
from pathlib import Path
def checkdir(directory, extension='*.nmconnection'):
try:
next(Path(directory).glob(extension))
return True
except StopIteration:
return False
checkdir('/etc/NetworkManager/system-connections')
英文:
Using Path from pathlib and its glob functionality you could do this:
from pathlib import Path
def checkdir(directory, extension='*.nmconnection'):
try:
next(Path(directory).glob(extension))
return True
except StopIteration:
return False
checkdir('/etc/NetworkManager/system-connections')
答案2
得分: 0
根据我的了解,要实现检查指定目录中是否存在一个或多个具有 "*.nmconnection" 文件扩展名的文件的目标,您可以稍微修改您的代码。不要打印文件名,而是如果找到任何这样的文件,可以将一个标志设置为 True,然后跳出循环。
您可以使用 pathlib 来完成这个任务!
英文:
To my knowledge To achieve your goal of checking if there are one or more files with the "*.nmconnection" file extension in the specified directory, you can modify your code slightly. Instead of printing the filenames, you can set a flag to True if any such file is found and then break out of the loop
You can use pathlib to do this!
答案3
得分: -1
你可以使用 os.walk()
,它会返回根目录、根目录内的子目录以及这些子目录中的文件。
import os
os.chdir("/etc/NetworkManager/system-connections/")
for (root, dirs, files) in os.walk("."):
for file in files:
if file.endswith(".nmconnection"):
print(file)
英文:
You can use os.walk()
which returns root directory, directories inside root directories and files inside those directories.
import os
os.chdir("/etc/NetworkManager/system-connections/")
for (root, dirs, files) in os.walk("."):
for file in files:
if file.endswith(".nmconnection"):
print(file)
答案4
得分: -1
我会使用`[pathlib][1]`来解决这个问题 -
```python
from pathlib import Path
directory = "/etc/NetworkManager/system-connections/"
suffix = "nmconnection"
def file_exists(directory, suffix):
p = Path(directory)
files_generator = p.rglob(f"*.{suffix}")
try:
next(files_generator)
return True
except:
return False
<details>
<summary>英文:</summary>
I would use the `[pathlib][1]` to solve this -
from pathlib import Path
directory = "/etc/NetworkManager/system-connections/"
suffix = "nmconnection"
def file_exists(directory, suffix):
p = Path(directory)
files_generator = p.rglob(f"*.{suffix}")
try:
next(files_generator)
return True
except:
return False
[1]: https://docs.python.org/3/library/pathlib.html#module-pathlib
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论