从文本文件重命名文件[关闭]

问题描述 投票:0回答:1

我想将 *.pdf 重命名为 myname.pdf 我的名字应该是来自last.txt的值

我在互联网上搜索如何做到这一点,但我是Python新手 我现在有这个代码,我想从创建的 txt 文件重命名文件

import os
import time
import subprocess
from pathlib import Path

tytul = 'titel'
numer = '1234567'
maszyna = '_'
wykonczenie = 'error'
kolor = 'error'
ilosc = '1'
wybor = input ('Content Digital(1) | Content Inkjet(2) | Cover(3): ')
if wybor == '1':      
        numer = input('Wpisz numer zlecenia: ')
        express = input('ZWYKLE(1) | EXPRESS(2)')
        if express == '1':
            express = ''
        elif express == '2':
            express = 'EXPRESS'
        else:
            print('blad')
        maszyna = input('Wpisz maszyne HP(1) KM(2) CANNON(3): ')
        if maszyna == '1':
            maszyna = 'CONTENT'
        elif maszyna == '2':
            maszyna = 'KM-1-CONTENT'
        elif maszyna == '3':
            maszyna = 'IX3200-CONTENT'
        else:
            print('blad')
        wykonczenie = input('Szycie(1) | klejenie(2) | WIO(3) (: ')
        if wykonczenie == '1':
            wykonczenie = 'SWB'
        elif wykonczenie == '2':
            wykonczenie = 'PB'
        elif wykonczenie == '3':
            wykonczenie = 'WIO'
        papier = input('Papier: ')
        kolor = input('Kolor: ')
        ilosc = input('Ilosc: ')
        with open('Ostatnie zlecenie.txt', 'w') as myFile:
            print(numer,express,maszyna,wykonczenie,papier,kolor,ilosc, sep="_", file=myFile, flush=False)
        with open('Wszystkie zlecenia.txt', 'a') as myFile:
            print(numer,express,maszyna,wykonczenie,papier,kolor,ilosc, sep="_", file=myFile, flush=False)
        stars_file = Path.cwd() / 'Ostatnie zlecenie.txt'
        subprocess.run(["C:/Windows/System32/notepad.exe", str(stars_file)]) ....
python file rename
1个回答
0
投票

您可以使用

endswith()


a = 'asdjfla92374981r708s45^7jsd fa.pdf'
b = 'asudauyfoaiu__YUHG.last.txt'

if a.endswith('.pdf') and b.endswith('.txt'):
    a = b[:-3] + "txt"

print(a)

打印

asudauyfoaiu__YUHG.last.txt

© www.soinside.com 2019 - 2024. All rights reserved.