![](/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.