2017-01-06 43 views
1

我正在編寫一個Python腳本來查找和刪除具有相應.pyc文件的所有.py文件。 如何提取此文件列表並將其刪除?使用python腳本查找和刪除文件

例如:考慮在有/富/酒吧一些文件:

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

我想刪除file.py,file2.py而不是file3.py,因爲它沒有相應的.pyc文件的文件。 ,我想在'/'下的所有文件夾中進行操作。

是否有同樣的單線程bash代碼?

PS:我用的CentOS 6.8,具有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 scriptiing中的新手。

0

這是一個非常操作系統-近固溶

也許從下面的命令做一個shell腳本,並使用subprocess.call從蟒蛇調用它(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

以下代碼適用於單層目錄。 (注意:我不知道你是如何處理多層文件夾---例如,如果你在一個文件夾中有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