0

私はpythonの初心者であり、自分のプログラムに自分のやりたいことをさせることができないようです。ユーザーが開きたい.gifファイルを指定すると、プログラムは指定したファイルを表示し.gifます。私が間違っているのかわかりません。誰かが私を助けてくれれば、とても感謝しています。ファイルを指定してもらいたいので、プログラムはファイルの名前をフォルダー内の「変数」に変更します。"variable.gif"呼び出されたファイルを開く/path/path/variable.gif

files = listdir(".")

print("List of GIF files in Directory:")
for f in files:
    if f[-4:] == ".gif":
        print(f)
print("type quit to close the program")     
gif1 = input("type which .gif you would like to modify and press enter:")
infile = open(gif1, "r")
outfile= open("gif1.gif", "w")
os.system('eog {0}'.format(r'home/pictures/gif1'))
4

1 に答える 1

0

以下のコード スニペットを試して、動作するかどうかお知らせください

import os

files = os.listdir(r'/home/pictures/')

print("List of GIF files in Directory:")
for f in files:
    if f.endswith('.gif'):
        print(f)
print("type quit to close the program")     
gif1 = input("type which .gif you would like to modify and press enter:")

if gif1 in files:
    os.system(r'eog /home/pictures/{0}'.format(gif1))
else:
    print 'The file name you entered is incorrect

プログラムeogは、開かれたファイル ハンドルではなくファイル名のみを想定しているため、投稿したコードは機能しません。また、 gif1はユーザーによって入力されるため、gif1のみを文字列/home/pictures/に追加する必要があります。

open(filename,'r')実際に取得するのはファイルハンドルです。このファイル ハンドルは、コマンド ライン引数として別のプログラムに渡すことはできません。また、別のポイントは、 /home/picturesのようなものを~/pictures/のようなものに置き換えることです。

于 2013-10-27T15:14:51.417 に答える