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