2

I have the following code. It works for the first directory but not the second one... What I am trying to do is to count the lines on each of the files in different directory.

import csv
import copy
import os
import sys
import glob

os.chdir('Deployment/Work/test1/src')
names={}
for fn in glob.glob('*.c'):
    with open(fn) as f:
        names[fn]=sum(1 for line in f if line.strip() and not line.startswith('/') and not line.startswith('#') and not line.startswith('/*')and not line.startswith(' *'))    

print ("Lines test 1 ", names)
test1 = names

os.chdir('Deployment/Work/test2/src')
names={}
for fn in glob.glob('*.c'):
    with open(fn) as f:
        names[fn]=sum(1 for line in f if line.strip() and not line.startswith('/') and not line.startswith('#') and not line.startswith('/*')and not line.startswith(' *'))    

print ("Lines test 2 ", names)
test2 = names

print ("Lines ", test1  + test2)

Traceback:

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Deployment/Work/test2/src'

3 Answers 3

4

You'll either have to return to the root directory using as many .. as required, store the root directory or specify a full directory from your home:

curr_path = os.getcwd()
os.chdir('Deployment/Work/test2/src')

os.chdir(curr_path)
os.chdir('Deployment/Work/test2/src')

Or:

os.chdir('Deployment/Work/test2/src')

os.chdir('../../../../Deployment/Work/test2/src') # Not advisable

Instead of the above, you may consider more Pythonic ways to change directories on the fly, like using a context manager for directories:

import contextlib
import os

@contextlib.contextmanager
def working_directory(path):
    prev_cwd = os.getcwd()
    os.chdir(path)
    yield
    os.chdir(prev_cwd)

with working_directory('Deployment/Work/test1/src'):
    names = {}
    for fn in glob.glob('*.c'):

with working_directory('Deployment/Work/test2/src'):
    names = {}
    for fn in glob.glob('*.c'):
        ...

You simply specify the relative directory from the current directory, and then run your code in the context of that directory.

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

Comments

2

Your os.chdir is interpreted relative to the current working directory. Your first os.chdir changes the working directory. The system tries to find the second path relative to the first path.

There are several ways to solve this. You can keep track of the current directory and change back to it. Else make the second os.chdir relative to the first directory. (E.g. os.chdir(../../test2/src'). This is slightly ugly. A third option is to make all paths absolute instead of relative.

Comments

0

I suppose the script is not working because you are trying to change the directory using a relative path. This means that when you execute the first os.chdir you change your working directory from the current one to 'Deployment/Work/test1/src' and when you call os.chdir the second time the function tries to change working directory to 'Deployment/Work/test1/src/Deployment/Work/test2/src' that I suppose is not what you want.

To solve this you can either use an absolute path:

os.chdir('/Deployment/Work/test1/src')

or before the first os.chdir you could keep track of your current folder:

current = os.getcwd() os.chdir('Deployment/Work/test1/src') ... os.chdir(current) os.chdir('Deployment/Work/test2/src')

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.