-1

I have a function that I use to organize files into folders. However, I would like to be able to have this function also recursively convert the files from DOS to UNIX as part of the routine. I know about the dos2unix command, but I keep getting a syntax error when I try to use it (dos2unix file SyntaxError: invalid syntax). I'm not sure why though.

Here's the function I'm running

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")   

Any suggestions?

Thanks!

Victor Murcia
  • 25
  • 1
  • 6
  • how do you call? dos2unix -u filename works fine. – Slawomir Dziuba Jun 12 '20 at 07:38
  • You blandly entered the name of an external program in the middle of a Python script. That is actually not how it works. – Jongware Jun 12 '20 at 08:00
  • Does this answer your question? [How to convert dos2unix csv file with python script](https://stackoverflow.com/questions/58363093/how-to-convert-dos2unix-csv-file-with-python-script) – Jongware Jun 12 '20 at 08:00
  • What do you mean by external program? Because the dos2unix command is a part of python that I have already installed https://pypi.org/project/dos2unix/ – Victor Murcia Jun 12 '20 at 08:02
  • Writing this in Python looks like reinventing the wheel. Recursive change of the file format from dos to unix can be done on one line in a shell: find dosdir -type f -exec dos2unix -u {} \; – Slawomir Dziuba Jun 12 '20 at 08:04
  • Yeah, I was just wondering if there was a way to do it easily in Python so that I could incorporate it as part of my main routine. I guess I'll just go ahead and do the extra step in Unix. – Victor Murcia Jun 12 '20 at 08:08
  • I ended up going with this option since I could then convert all of the .run files in all of the subdirectories I had created while in Linux find . -type f -name "*.run" -exec dos2unix {} \+; Thanks for pointing me in the right direction Slawomir. – Victor Murcia Jun 12 '20 at 08:35

1 Answers1

0

Recursively for all files in the dosdir directory.

import os
os.system("find dosdir -type f -exec dos2unix -u {} \; ");

requires the find and dos2unix system commands, mostly this is true by default.

Slawomir Dziuba
  • 1,265
  • 1
  • 6
  • 13
  • I ended up going with this option since I could then convert all of the .run files in all of the subdirectories I had created while in Linux find . -type f -name "*.run" -exec dos2unix {} \+; Thanks for pointing me in the right direction Slawomir. – Victor Murcia Jun 12 '20 at 08:39