03 Python 文件操作
文件操作
获取或创建文件夹
import os
def get_or_create_folder():
base_folder = os.path.abspath(os.path.dirname(__file__))
folder = 'pictures' # 需要获取或打开的文件夹
full_path = os.path.join(base_folder, folder)
if not os.path.exists(full_path) or not os.path.isdir(full_path):
print('Creating new directory at {}'.format(full_path))
os.mkdir(full_path)
return full_path打开文件或文件夹
import platform
import subprocess
def open_folder(folder):
print('Displaying cats in OS window.')
os_name = platform.system()
if os_name == 'Darwin':
subprocess.call(['open', folder])
elif os_name == 'Windows':
subprocess.call(['explorer', folder])
elif os_name == 'Linux':
subprocess.call(['xdg-open', folder])
else:
print("We don't support your os: " + os_name)下载图片
最后更新于