简体   繁体   English

无法在python中更改文件夹路径但我不知道为什么

[英]Failed to change folder path in python but I do not know why

I want to open the font in folder 1 and folder 2. I wrote openOTF.py to open them.我想打开文件夹 1 和文件夹 2 中的字体。我写了openOTF.py来打开它们。

Desktop 
├── openOTF.py
├── 1
│   ├── CMBSY7.otf
│   ├── CMBSY8.otf
│   └── CMBSY9.otf
├── 2
│   ├── CMCSC8.otf
│   └── CMCSC9.otf

this is openOTF.py这是 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("../")

but this is the output of python open-otf.py , which cannot find CMCSC8.otf in folder 2 , in fact it want to search /home/firestar/Desktop/1/CMCSC8.otf :但这是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

it seems that os.chdir("../") changed the path to folder 2 but fontforge did not change the path (still in folder 1 ).似乎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)

I think glob is the simplest solution我认为glob是最简单的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个hdfs路径,我怎么知道它是一个文件夹还是一个带有python的文件 - Given a hdfs path, how do I know if it is a folder or a file with python 了解Python中的配置文件夹路径 - Know the config folder path in Python 这是否意味着pip安装的python路径不正确? 如果是这样,我如何知道正确的路径,然后如何更改? - Does this means the python path for pip install is incorrect? If so, how do I know the right path and then how do I change that? 我如何知道 linux ubuntu 上的 python 路径? - How do I know python path on linux ubuntu? 为什么在Python路径中需要4个反斜杠? - Why do I need 4 backslashes in a Python path? 如何找到失败的python导入的路径? - How do I find the path for a failed python import? Python - 如果我不知道扩展名,如何在文件夹中找到文件? - Python - How do I find a file in a folder if I don't know the extension? 如何在Python中正确更改文件路径的名称? - How do I change the name of a file path correctly in Python? 如何在Python中更改/选择文件路径? - How do I change/choose the file path in Python? 我删除了anaconda,现在我的python路径搞砸了。 你知道怎么解决吗? - I removed anaconda and now my python path is messed up. Do you know how to fix it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM