折腾:
【未解决】mac中如何获取electron的app的根目录
期间,想要Python实现类似于touch一样,创建一个空文件。
python create empty file
【总结】
代码:
def createEmptyFile(fullFilename): """Create a empty file like touch""" filePath = os.path.dirname(fullFilename) # create folder if not exist if not os.path.exists(filePath): os.makedirs(filePath) with open(fullFilename, 'a'): # Note: not use 'w' for maybe conflict for others constantly writing to it os.utime(fullFilename, None)
后续看到:
对于 Python 3.4+ ,最好用:
from pathlib import Path Path(fullFilename).touch()
调用:
createEmptyFile(self.fileToSave)
效果:

【后记20201027】
更新注释:
def createEmptyFile(fullFilePath): """Create a empty file like touch Args: fullFilePath (str): full file path Returns: bool Raises: """ folderPath = os.path.dirname(fullFilePath) # create folder if not exist if not os.path.exists(folderPath): os.makedirs(folderPath) with open(fullFilePath, 'a'): # Note: not use 'w' for maybe conflict for others constantly writing to it os.utime(fullFilePath, None)
最新代码:
转载请注明:在路上 » 【已解决】Python中实现类似touch创建一个空文件