2

I am writing a python script in which I read a text file and I want to create a new text file in another directory. I wrote the following code:

def treatFiles(oldFile, newFile):   
    with open(oldFile) as old, open(newFile, 'w') as new:
        count = 0
        for line in old:
            count += 1
            if count%2 == 0:
                pass
            else:
                new.write(line)

if __name__ == '__main__':
    from sys import argv
    import os
    os.makedirs('NewFiles')
    new = '/NewFiles/' + argv[1]
    treatFiles(argv[1], new)

I tried running this code with a text file in the same directory than my python script, but got an error like

FileNotFoundError: [Errno 2] No such file or directory: '/NewFiles/testFile'

Apparently, it is not clear that NewFiles is a directory in which it should create the new file... How can I correct this?

1
  • 3
    add the newfiles directory to the root folder (or get rid of the / in front of it) Commented Sep 22, 2013 at 2:43

1 Answer 1

3

The problem is actually that in unix, /NewFiles/ means a folder in the root directory called NewFiles, not in the current directory. Remove the leading / and it should be fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.