gpt4 book ai didi

python - 从python中的命令行参数获取文件路径

转载 作者:太空狗 更新时间:2023-10-29 19:32:28 26 4
gpt4 key购买 nike

如果我们从命令行参数传递文件并提取文件,谁能指导我如何获取文件路径。如果我们还需要检查文件是否存在于特定目录中

python.py /home/abhishek/test.txt

获取文件路径并检查 test.txt 是否存在于 abhishek 文件夹中。

我知道这可能很容易,但我对 pytho 有点陌生

最佳答案

import os
import sys

fn = sys.argv[1]
if os.path.exists(fn):
print os.path.basename(fn)
# file exists

关于python - 从python中的命令行参数获取文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14360389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com