문제

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 .

도움이 되었습니까?

해결책

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)

다른 팁

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

Analogous to

dir,file = os.path.split(input)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top