Question

I have been using the Automator on OSX [according to previous questions] and my os.path.split is not working for some reason even though it is the same code that runs fine in CodeRunner and terminal...

Is there any other way to achieve this function:

import os
input = '/Users/Opus_Magnum/desktop/list.txt'
output = 'test_output.txt'

dir,file = os.path.split(input)
temp_out= os.path.join(dir,output)
out_file=open(temp_out,'w')

print dir
print file
print temp_out

>> /Users/Opus_Magnum/desktop
>> list.txt
>> /Users/Opus_Magnum/desktop/test_output.txt

I'm basically trying to create a new file in the same directory as the input file

I would prefer to know what could be causing my os.path.split function to not work but if something analogous is available then I guess that can work too .

Was it helpful?

Solution

You can use os.path.dirname() to obtain the directory of the input file.

input = '/Users/Opus_Magnum/Desktop/list.txt'
input_dir = os.path.dirname(input)
output = 'test_output.txt'
temp_out = os.path.join(input_dir, output)

OTHER TIPS

dir = os.path.dirname(input)
file = os.path.basename(input) 

Analogous to

dir,file = os.path.split(input)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top