[英]Recursively convert DOS files to UNIX format in Python
我有一個 function 用於將文件組織到文件夾中。 但是,我希望能夠讓這個 function 也遞歸地將文件從 DOS 轉換為 UNIX 作為例程的一部分。 我知道 dos2unix 命令,但是當我嘗試使用它時不斷收到語法錯誤(dos2unix 文件 SyntaxError:無效語法)。 我不確定為什么。
這是我正在運行的 function
def listFiles(aname,nAtoms): #This function organizes the .run files for all atoms into folders
iniPath = os.getcwd()
runfiles = []
folders = []
filenames= os.listdir (".")
#This populates the list runfiles with any files that have a .run extension
for file in glob.glob("*.run"):
dos2unix file
runfiles.append(file)
#This populates the list folders with any folders that are present in the current working directory
for file in filenames:
if os.path.isdir(os.path.join(os.path.abspath("."), file)):
folders.append(file)
#Perform a natural sort of the files and folders (i.e. C1,C2,C3...C10,C11,etc, instead of C1,C10,C11...C2,C3)
natSortKey = natsort_keygen(key=lambda y: y.lower(), alg=ns.IGNORECASE)
runfiles.sort(key = natSortKey)
folders.sort(key = natSortKey)
#This loop moves the files to their respective atom folders and deletes the version in the original directory
i = 1
nf = 0
for i in range(0,nAtoms + 1):
atomDir = aname + str(i)
for item in runfiles:
if item.startswith(atomDir):
if nf >= i*5:
break
else:
shutil.copy(path.join(iniPath,item),atomDir)
os.remove(item)
nf += 1
print("The files are:")
print(runfiles,"\n")
print("The folders are:")
print(folders,"\n")
有什么建議么?
謝謝!
遞歸處理 dosdir 目錄中的所有文件。
import os
os.system("find dosdir -type f -exec dos2unix -u {} \; ");
需要 find 和 dos2unix系統命令,大多數情況下默認情況下是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.