質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

3回答

856閲覧

pythonで同じフォルダにあるものを一括操作する方法について。

yusuke1818

総合スコア10

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/12/18 02:09

編集2020/12/18 02:57

pythonで、同じフォルダにあるentファイルをtkinterで入力をさせると、そのファイルを開き、そのテキストの中から数字の範囲を抽出するプログラムはできたのですが、一つ一つファイル名を入力させるのはたくさんあるので手間がかかるので、同じフォルダの中のファイルを自動ですべて読み込むようにしたいのですが、どのようにすればよろしいでしょうか?わかりません。教えてください。お願いいたします。

import glob # テキストファイルのみを取得 glob.glob("*.ent")

で.entのみをリスト化してファイル名を取得取得する方法は分かりました。しかしそれを下記のコードのどこに入れればいいのか、そしてそれを順番に入力させて実行させるやり方が分かりません。
お願いします。

import tkinter from selenium import webdriver from time import sleep import requests from bs4 import BeautifulSoup import more_itertools as mit root=tkinter.Tk() root.title(u"CA") root.geometry("500x500") Static1 = tkinter.Label(text=u'タンパク質') Static1.pack() EditBox = tkinter.Entry(width=50) EditBox.insert(tkinter.END,"") EditBox.place(x=170,y=100) EditBox.pack() def btn_click(): path=EditBox.get() print("---",EditBox.get(),"---") serial_numbers_A = [] serial_numbers_B = [] def consecutive_groups(iterable): for group in mit.consecutive_groups(iterable): group = list(group) if len(group) == 1: yield group[0] else: yield group[0], group[-1] with open(path) as f: lines = f.readlines() lines_strip = [line.strip() for line in lines] #A側のCAの抽出 for A_and in lines: A_and=A_and.split() if A_and[0] == 'ATOM' and A_and[2] == 'CA' and A_and[4] == 'A': serial_numbers_A.append(int(A_and[5])) print("A側のCA") for Agroup in consecutive_groups(serial_numbers_A): print(list(Agroup)) #B側のCAの抽出 for B_and in lines: B_and=B_and.split() if B_and[0] == 'ATOM' and B_and[2] == 'CA' and B_and[4] == 'B': serial_numbers_B.append(int(B_and[5])) print(" ") print("B側のCA") for Bgroup in consecutive_groups(serial_numbers_B): print(list(Bgroup)) print(" ") print(Bgroup[0],Bgroup[1],Bgroup[1]-Bgroup[0]+Bgroup[0]) btn = tkinter.Button(root, text='タンパク質を調べる', command=btn_click) btn.place(x=170, y=200) root.mainloop()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

自己解決

import tkinter from selenium import webdriver from time import sleep import requests from bs4 import BeautifulSoup import more_itertools as mit import glob # テキストファイルのみを取得 listA=glob.glob("*.ent") print(listA) for name in listA: def consecutive_groups(iterable): """Yield range of consecutive numbers.""" for group in mit.consecutive_groups(iterable): group = list(group) if len(group) == 1: yield group[0] else: yield group[0], group[-1] path = name print("--------",name,"--------") serial_numbers_A = [] serial_numbers_B = [] with open(path) as f: lines = f.readlines() lines_strip = [line.strip() for line in lines] #A側のCAの抽出 for A_and in lines: A_and=A_and.split() if A_and[0] == 'ATOM' and A_and[2] == 'CA' and A_and[4] == 'A': serial_numbers_A.append(int(A_and[5])) print("A側のCA") for Agroup in consecutive_groups(serial_numbers_A): print(list(Agroup)) #B側のCAの抽出 for B_and in lines: B_and=B_and.split() if B_and[0] == 'ATOM' and B_and[2] == 'CA' and B_and[4] == 'B': serial_numbers_B.append(int(B_and[5])) print(" ") print("B側のCA") for Bgroup in consecutive_groups(serial_numbers_B): print(list(Bgroup)) print("------------------------------------------")

投稿2020/12/18 04:33

yusuke1818

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

標準ライブラリのglobを使用すると
指定したディレクトリの任意の拡張子のファイルが一括で取得できます。
あとはそれらを一つずつ処理していけばよいでしょう。

python

1import os 2import glob 3 4EditBox = tkinter.Entry(width=50) 5EditBox.insert(tkinter.END,"") 6EditBox.place(x=170,y=100) 7EditBox.pack() 8 9def btn_click(): 10 value = EditBox.get() 11 # [ディレクトリ名] + [ワイルドカードを使用した拡張子の指定]の結合 12 path = os.path.join(value, '*.ent') 13 file_list = glob.glob(path) # 指定したディレクトリ内のentファイルを全て取得 14 15 16btn = tkinter.Button(root, text='タンパク質を調べる', command=btn_click) 17btn.place(x=170, y=200)

投稿2020/12/18 02:45

nto

総合スコア1438

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yusuke1818

2020/12/18 02:51

TclError Traceback (most recent call last) <ipython-input-35-c7e86ff02b9a> in <module> 19 20 ---> 21 btn = tkinter.Button(root, text='タンパク質を調べる', command=btn_click) 22 btn.place(x=170, y=200) C:\ANACONDA\lib\tkinter\__init__.py in __init__(self, master, cnf, **kw) 2367 overrelief, state, width 2368 """ -> 2369 Widget.__init__(self, master, 'button', cnf, kw) 2370 2371 def flash(self): C:\ANACONDA\lib\tkinter\__init__.py in __init__(self, master, widgetName, cnf, kw, extra) 2297 del cnf[k] 2298 self.tk.call( -> 2299 (widgetName, self._w) + extra + self._options(cnf)) 2300 for k, v in classes: 2301 k.configure(self, v) TclError: can't invoke "button" command: application has been destroyed とのエラーが出て所得出来ません。どういうことでしょうか。
guest

0

「python ファイル一覧」とかでググれば色々出てきます。たとえば、標準ライブラリのosを使ってlistdirでファイル一覧を取得する方法などがあります。
【Python入門】listdir関数でデータを取得する方法

投稿2020/12/18 02:26

編集2020/12/18 02:27
jeanbiego

総合スコア3966

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yusuke1818

2020/12/18 02:52

ありがとうございます。 取得したものをリスト化できました。 それを順番に実行させるやり方が分かりません。お願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問