Python os.path.join() - ไม่สามารถรวมได้อย่างถูกต้อง

ฉันต้องการบันทึกไฟล์บางไฟล์ในไดเร็กทอรีย่อยของไดเร็กทอรีปัจจุบัน แต่ด้วยเหตุผลใดก็ตาม แม้ว่า Python จะ "ประสบความสำเร็จ" ในการบันทึกไฟล์เอาต์พุต แต่ไดเร็กทอรีก็ไม่ปรากฏที่ใดเลย

com_path = './'
out_f = "output"
crop_f = "1-cropped"
(…)
curr_path = os.path.join(com_path, out_f, crop_f)
for elem in images:
    img = img_trim(elem['img'])
    filename = curr_path + 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem
    cv2.imwrite(filename, img)
print(curr_path)
>>> ./output\1-cropped
print(com_path)
>>> ./

น่าแปลกที่ในขณะที่ใช้งาน (เดิมทีฉันเขียนที่นี่ curr_path แทนที่จะเป็น com_path ซึ่งเป็นข้อผิดพลาด):

filename = com_path + 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem

ทุกอย่างทำงานได้ดีและจะบันทึกไฟล์ที่ต้องการไว้ในโฟลเดอร์สคริปต์ .py

จากสิ่งที่ฉันรวบรวมจากกระทู้อื่นๆ และเอกสารอย่างเป็นทางการ มันควรจะได้ผล—ทำไมถึงไม่ได้ล่ะ?

แก้ไข: ตามคำขอ:

print(os.getcwd())
>>> C:\Users\iyoossaev\Desktop\Index Cards\tester

มันควรจะอยู่ที่ไหน

print(filename)
>>> ./p_71, (p0089).png
>>> ./p_72, (p0090).png
>>> ./p_73, (p0091).png
>>> ./p_74, (p0092).png

ไฟล์ไม่ว่างเปล่า เมื่อฉันใช้ "com_path" ทุกอย่างทำงานได้ (ทั้งการโหลดและการบันทึก) ไฟล์จะถูกสร้างขึ้นตามที่คาดไว้ การเปลี่ยนไดเร็กทอรีการบันทึกจะทำให้สิ่งต่างๆ ยุ่งเหยิง (โดยไม่ต้องเปลี่ยนไดเร็กทอรีการโหลด)

แก้ไข 2: ฉันตัดสินใจวางโค้ดทั้งหมดเพื่อความแน่ใจ โปรแกรมนี้มีไว้สำหรับโหลดรูปภาพ แปลงเป็นขาวดำ ค้นหาข้อความที่เกี่ยวข้องโดยใช้พิกเซลเฉลี่ยในแถว/คอลัมน์ และตัดแต่ง จากนั้นบันทึกผลลัพธ์ด้วยชื่อที่แก้ไข ทุกอย่างทำงานได้ยกเว้นตอนที่ฉันพยายามใส่มันลงในโฟลเดอร์ย่อย ภาพตัวอย่างที่จะทดสอบ: https://imgur.com/X2mPZr0

# IndexCardSplitter
import cv2      # biblioteka do obróbki 
import os       # operating system-specific
import re       # RegEx
import numpy    # obliczenia matematyczne

p_num_off = -18 
t_thresh_hor = 210.0
t_thresh_ver = 240.0
t_thresh_m = 25


com_path = os.getcwd()
# com_path = '.'
filenames = os.listdir(com_path)
filenames = [ f for f in filenames if os.path.isfile(os.path.join(com_path, f)) and re.match('[^\_]*\.png$', f) ]

out_f = "output"
crop_f = "1-cropped"
col_f = "2-columns"
def_f = "3-definitions"


def name_mod(filename):
    elements = re.match("(p0{1,}([1-9][^\.]*?))(\.....?$)", filename)
    orig_name = elements.group(1)
    p_num = str(int(elements.group(2)) + p_num_off)
    ext = elements.group(3)
    return{'orig_name':orig_name, 'p_num':p_num, 'ext':ext}


def load_images(filenames):
    images = []
    for f in filenames:
        print(f)
        img_t = cv2.imread(os.path.join(com_path,f), cv2.IMREAD_GRAYSCALE)
        thresh, img_t = cv2.threshold(img_t, 128, 255, cv2.THRESH_OTSU)
        dictionary = name_mod(f)
        dictionary['img'] = img_t
        images.append(dictionary)
    return images


def img_trim(image):
    height, width = image.shape
    height = height - 1
    width = width - 1

    top_trim = 0
    bot_trim = height
    left_trim = 0
    right_trim = width

    i = 0           # GÓRA
    while i < height and top_trim == 0:
        row = image[i, 0:-1]
        if numpy.mean(row) < t_thresh_hor:
            top_trim = i
        i = i + 1

    i = height      # DÓŁ
    while i > 0 and bot_trim == height:
        row = image[i, 0:-1]
        if numpy.mean(row) < t_thresh_hor:
            bot_trim = i
        i = i - 1

    i = 0           # LEWO
    while i < width and left_trim == 0:
        col = image[0:-1, i]
        if numpy.mean(col) < t_thresh_ver:
            left_trim = i
        i = i + 1

    i = width       # PRAWO
    while i > 0 and right_trim == width:
        col = image[0:-1, i]
        # col = image[0:-1, 2700]
        if numpy.mean(col) < t_thresh_ver:
            right_trim = i
        i = i - 1

    top_trim = top_trim - t_thresh_m
    bot_trim = bot_trim + t_thresh_m
    left_trim = left_trim - t_thresh_m
    right_trim = right_trim + t_thresh_m

    image = image[top_trim:bot_trim, left_trim:right_trim]
    return(image)


print('*'*40)

# curr_path = os.path.join(os.getcwd(), out_f, crop_f)
# print(curr_path)
print(com_path)
for elem in images:
    img = img_trim(elem['img'])
    # filename = os.path.join(os.getcwd(), out_f, crop_f, 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem)
    filename = com_path + 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem
    print(filename)
    cv2.imwrite(filename, img)

ชื่อไฟล์อินพุตคือ: p0089.png, p0090.png เป็นต้น


person MrVocabulary    schedule 14.02.2017    source แหล่งที่มา
comment
บางที images อาจเป็นรายการว่างใช่ไหม ใส่ print(filename) ไว้ในวงของคุณ หรือบางทีไดเร็กทอรีการทำงานปัจจุบันของคุณอาจไม่ใช่สิ่งที่คุณคาดหวัง วาง print(os.getcwd()) ไว้ที่ไหนสักแห่ง หรือโปรดแก้ไขคำถามของคุณเพื่อรวมโปรแกรมสั้นๆ สมบูรณ์ที่แสดงให้เห็นถึงปัญหา ดูตัวอย่างการทำซ้ำขั้นต่ำสำหรับข้อมูลเพิ่มเติม   -  person Robᵩ    schedule 15.02.2017
comment
นั่นควรจะเป็น filename = os.path.join(curr_path, 'p_%(p_num)s, ...'%elem) ไม่ใช่เหรอ?   -  person Aran-Fey    schedule 15.02.2017
comment
ปรับปรุงตามคำขอ @Rawing การทำสิ่งที่คุณแนะนำเป็นวิธีดั้งเดิมของฉัน แต่มันก็ทำสิ่งเดียวกันนั่นคือไม่มีข้อผิดพลาดส่งคืน ดูเหมือนว่าทุกอย่างเรียบร้อยดี แต่ไม่พบโฟลเดอร์/ไฟล์ที่ไหนเลย   -  person MrVocabulary    schedule 15.02.2017
comment
คุณได้ลองแทนที่ com_path = './' ด้วย com_path = 'os.getcwd()' แล้วหรือยัง? ไม่อย่างนั้น คุณคงอยากแน่ใจว่า './' แก้ไขอะไร!   -  person Son of a Beach    schedule 15.02.2017
comment
@Son of a Beach ฉันมีและมันให้ผลลัพธ์แปลก ๆ โปรดดูโพสต์ที่อัปเดตของฉันและคำตอบของฉันต่อ Dmitry Rubanovich   -  person MrVocabulary    schedule 15.02.2017


คำตอบ (1)


ลองตั้งค่า com_path เป็น '." หรือ os.getcwd() os.path.join ไม่ได้แทนที่เครื่องหมายทับด้วย os.sep

person Dmitry Rubanovich    schedule 14.02.2017
comment
การใช้ com_path = os.getcwd() ทำให้ฉันได้ผลลัพธ์การพิมพ์ต่อไปนี้: C:\Users\iyoossaev\Desktop\Index Cards\testerp_71, (p0089).png แต่การใช้ '.' ทำงานได้ดี ทำให้ฉันพิมพ์: .p_71, (p0089).png (และปรากฏโดยมีจุดนั้นอยู่ด้านหน้าใน dir ที่ใช้งานได้) การใช้ filename = os.path.join(os.getcwd(), out_f, crop_f, 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem) พิมพ์เส้นทางที่สมบูรณ์แบบ แต่ไม่มีอะไรเกิดขึ้นกับไฟล์... - person MrVocabulary; 15.02.2017
comment
ดูเหมือนว่าคุณเปลี่ยนไปใช้ + แทนที่จะเป็น os.path.join os.path.join จะเชื่อมองค์ประกอบเส้นทางและชื่อไฟล์เข้าด้วยกันโดยใช้ตัวคั่นเฉพาะระบบ (os.sep) มันคือ '\' ใน Windows และ '/' ใน Unix ฉันไม่คิดว่า filename = com_path + 'p_%(p_num)s, (%(orig_name)s)%(ext)s' % elem จะใส่ตัวคั่น หากคุณต้องการทำด้วยมือและสร้างชื่อเส้นทางแบบเต็มด้วยตัวเอง คุณควรเพิ่มอักขระตัวคั่น (os.path) ระหว่างองค์ประกอบเส้นทางด้วย - person Dmitry Rubanovich; 16.02.2017
comment
โอเค ความคิดเห็นสุดท้ายของคุณคือสิ่งที่ไอ้โง่ของฉันต้องการ—ตอนนี้มันใช้งานได้ดี :) แต่ฉันก็สันนิษฐานด้วยว่าการเขียนไฟล์ไปยังโฟลเดอร์ย่อยที่ไม่มีอยู่จะสร้างมันขึ้นมาโดยอัตโนมัติ... - person MrVocabulary; 16.02.2017