![](/img/trans.png)
[英]Given a hdfs path, how do I know if it is a folder or a file with python
[英]Failed to change folder path in python but I do not know why
我想打开文件夹 1 和文件夹 2 中的字体。我写了openOTF.py
来打开它们。
Desktop
├── openOTF.py
├── 1
│ ├── CMBSY7.otf
│ ├── CMBSY8.otf
│ └── CMBSY9.otf
├── 2
│ ├── CMCSC8.otf
│ └── CMCSC9.otf
这是 openOTF.py
import fontforge as ff
import os
folders = ["1/", "2/"]
for folder in folders:
os.chdir(folder)
print(os.getcwd())
files = os.listdir("./")
for font in files:
print(font)
f = ff.open(font)
print(f.path)
os.chdir("../")
但这是python open-otf.py
的输出,它在文件夹2
中找不到CMCSC8.otf
,实际上它想搜索/home/firestar/Desktop/1/CMCSC8.otf
:
/home/firestar/Desktop/1
CMBSY9.otf
/home/firestar/Desktop/1/CMBSY9.otf
CMBSY8.otf
/home/firestar/Desktop/1/CMBSY8.otf
CMBSY7.otf
/home/firestar/Desktop/1/CMBSY7.otf
/home/firestar/Desktop/2
CMCSC8.otf
The requested file, CMCSC8.otf, does not exist
Traceback (most recent call last):
File "/home/firestar/Desktop/open-otf.py", line 12, in <module>
f = ff.open(font)
OSError: Open failed
似乎os.chdir("../")
更改了文件夹2
的路径,但 fontforge 没有更改路径(仍在文件夹1
中)。
import fontforge as ff
import glob
files = glob.glob('*/*.otf')
print(files)
for font in files:
f = ff.open(font)
print(f.path)
我认为glob
是最简单的解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.