python - 如何仅从 splitExt 输出扩展名?
问题描述
我有这个旧batch
代码用于在目录根目录中获取文件(未使用),并将它们放在目录中(使用文件名作为目录名):
for %%s in (*.*) do if not "%%~xs"==".!qB" if not "%%~xs"==".parts" (
md "%%~ns" & move "%%s" "%%~ns\"
if exist "%%s" rd "%%~ns"
)
这将获取一个d:\currentdir\filename.ext
文件并使用文件名将其嵌套到d:\currentdir\filename\filename.ext
中,除非文件扩展名是已知的临时文件,例如.!qB
or .parts
。
我正在尝试将其重写为python ..
rootfiles = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in rootfiles:
print(os.path.splitext(f))
我被困在了splitExt
,如何仅获取扩展变量,以便排除提到的已知临时扩展?我没有从文档中得到它。
解决方案
试试看:
rootfiles = [f for f in os.listdir('.') if os.path.isfile(f) and not f.endswith(".!qB") and not f.endswith(".parts")]