如何重命名文件,将文件保存在同一目录中?
我有一个包含文件完整路径的字符串,以及一个包含新文件名(并且没有路径)的字符串,例如:
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)