如何重命名文件并保留在同一目录中?
我有一个包含完整文件路径的字符串和一个包含新文件名(不包含路径)的字符串,例如:
NSString *old_filepath = @"/Volumes/blah/myfilewithrubbishname.avi";
NSString *new_filename = @"My Correctly Named File.avi";
我知道NSFileManager的movePath:toPath:handler:方法,但我不知道如何构建新文件的路径。
基本上,我正在寻找以下Python代码的等效代码:
>>> import os
>>> old_filepath = "/Volumes/blah/myfilewithrubbishname.avi"
>>> new_filename = "My Correctly Named File.avi"
>>> dirname = os.path.split(old_filepath)[0]
>>> new_filepath = os.path.join(dirname, new_filename)
>>> print new_filepath
/Volumes/blah/My Correctly Named File.avi
>>> os.rename(old_filepath, new_filepath)