2017-01-06 7 views
1

対応する.pycファイルを持つすべての.pyファイルを検索して削除するPythonスクリプトを作成しています。 このファイルリストを抽出して削除するにはどうすればよいですか?pythonスクリプトを使用したファイルの検索と削除

たとえば:/ fooの/バーにいくつかのファイルが考える:

file.py 
file.pyc 
file3.py 
file2.py 
file2.pyc...etc 

私はfile.py、file2.pyを削除し、それが対応する.pycファイルを持っていないようfile3.pyないようにしたいです。 と私は '/'の下のすべてのフォルダでやりたい

同じライナーのbashコードはありますか?

PS:私は、これは多分、次のコマンドからシェルスクリプトを作成し、subprocess.callを使ってPythonから呼び出す非常にオペレーティング・システムに近いソリューション

あるpython2.7

+0

興味深い要求。対応する '.py'ファイルを持つ' .pyc'ファイルを削除したくないのですか? :) –

+0

いいえ、それはクライアントの要求です –

答えて

1

は、ここに私のソリューションです:

import os 
    ab=[] 
    for roots,dirs,files in os.walk("/home/foo/bar/"): 
     for file in files: 
      if file.endswith(".py"): 
       ab.append(os.path.join(roots,file)) 
    bc=[] 
    for i in range(len(ab)): 
     bc.append(ab[i]+"c") 
    xy=[] 
    for roots,dirs,files in os.walk("/home/foo/bar/"): 
     for file in files: 
      if file.endswith(".pyc"): 
       xy.append(os.path.join(roots,file)) 
    ex=[x[:-1] for x in bc if x in xy] 
    for i in ex: 
     os.remove(i) 

PS:Pythonスクリプティングの初心者。

0

を持つ、CentOSの6.8を使用しています(How to call a shell script from python code?Calling an external command in Python

find . -name "*.pyc" > /tmp/pyc.txt

find . -name "*.py" > /tmp/py.txt

これらのファイルのエントリから

sedbasenameを使用して終わるパスとファイルを削除します。

for f in $(cat /tmp/pyc.txt) ; do sed 's/.*\///' remove path sed 's/\.[^.]*$//' remove file ending done

for f in $(cat /tmp/py.txt) ; do sed 's/.*\///' remove path sed 's/\.[^.]*$//' remove file ending done

https://unix.stackexchange.com/questions/44735/how-to-get-only-filename-using-sed

awk 'FNR==NR{a[$1];next}($1 in a){print}' /tmp/pyc.txt /tmp/py.txt > /tmp/rm.txthttps://unix.stackexchange.com/questions/125155/compare-two-files-for-matching-lines-and-store-positive-results

for f in $(cat /tmp/rm.txt) ; do rm $f doneUnix: How to delete files listed in a fileを)

+0

可能であれば、私は同じライナーコードが必要ですか? –

0

次のコードは、1つのレイヤーディレクトリに対して機能します。 (注:フォルダの複数のレイヤーをどのように処理するかはわかりませんでした。たとえば、あるフォルダにA.py、別のフォルダにA.pycがある場合は、両方が存在すると見なされますか、または同じフォルダ内にある必要がありますか?。同じフォルダの層後者の場合ならば、それはフォルダを単にループにかなり簡単なもので、ちょうど各ループ内でこのコードを呼び出す必要があります)

import os 

# Produces a sorted list of all files in a directory 
dirList = os.listdir(folder_path) # Use os.listdir() if want current directory 
dirList.sort() 

# Takes advantage of fact that both py and pyc files will share same name and 
# that pyc files will appear immediately after their py counterparts in dirList 

lastPyName = "" 

for file in dirList: 
    if file[-3:] == ".py": 
     lastPyName = file[:-3] 
    elif file[-4:] == ".pyc": 
     if lastPyName == file[:-4]: 
      os.remove(lastPyName + ".py") 
      os.remove(lastPyName + ".pyc") # In case you want to delete this too 
+0

コードが正常に動作します。しかし、時にはdirlist .pycファイルが最初に来る.py、 例: 'list [1] = argu.py list [2] = test1.pyc list [3] = self。py 。 。 。 list [10] = test.py' 比較中に問題が発生しますか? –

+0

dirlistの後にソートを追加してみてください。例えば。 'dirlist = sort(dirlist)'これは.pycが対応する.pyファイルの直後に来ることを保証するはずです。 – Elliptica

+0

明確にするために、Pythonソート構文は 'dirList.sort()'です。上記のコードに追加しました。 – Elliptica

1

バッシュ・ソリューション:

#!/bin/bash 

find /foo/bar -name "*.py" -exec ls {} \; > file1.txt 
find /foo/bar/ -name "*.pyc" -exec ls {} \; > file2.txt 
p=`wc -l file1.txt| cut -d' ' -f1` 
for ((c=1;c<=$p;c++)) 
do 
    grep `sed -n ${c}p file1.txt | sed s/$/c/g` file2.txt > /dev/null 
    if [ $? -eq 0 ] 
    then 
     list=`sed -n ${c}p file1.txt` 
      echo " exist : $list" 
      rm -rf `sed -n ${c}p file1.txt` 
    fi 
done 
関連する問題