顯示具有 os 標籤的文章。 顯示所有文章
顯示具有 os 標籤的文章。 顯示所有文章

2020年11月2日 星期一

取得執行py的路徑與工作路徑-Get the path of running file (.py) in Python

Code

'''
System version : Windows 10  64-bit  
Python version : Python 3.6.0 :: Anaconda 4.3.1 (64-bit)   
'''

import os
print('os.getcwd():', os.getcwd())
print('__file__:', __file__)
print('os.path.basename(__file__):', os.path.basename(__file__))
print('os.path.dirname(__file__):', os.path.dirname(__file__))
print('-'*45)

# 存原始的工作目錄
save_raw_path = os.getcwd()
print("存原始的工作目錄")
print("os.getcwd():",os.getcwd())
print('-'*45)

# 變更工作目錄
go_path = r"D:\tmp"
os.chdir(go_path) 
print("變更工作目錄")
print("os.getcwd():",os.getcwd())
print('-'*45)

'''execute your code here'''

# 改回原始的工作目錄
os.chdir(save_raw_path) 
print("改回原始的工作目錄")
print("os.getcwd():",os.getcwd())

Result

os.getcwd(): D:\project\實用程式庫\log紀錄
__file__: d:/project/實用程式庫/log紀錄/tmp.py
os.path.basename(__file__): tmp.py
os.path.dirname(__file__): d:/project/實用程式庫/log紀錄
---------------------------------------------
存原始的工作目錄
os.getcwd(): D:\project\實用程式庫\log紀錄
---------------------------------------------
變更工作目錄
os.getcwd(): D:\tmp
---------------------------------------------
改回原始的工作目錄
os.getcwd(): D:\project\實用程式庫\log紀錄

2017年7月26日 星期三

Python - How can I list the contents of a directory - 取得路徑下檔案名稱與資料夾名稱

Version

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
System version :Windows 10
分別取得資料夾路徑與檔案路徑

Code:

import os
dir_path = r'E:\download\tmp\test'
# 取得路徑下的檔案名稱與資料夾名稱
dir_list = os.listdir(dir_path)

# 取得路徑下的檔案名稱與完整路徑
only_file=[dir_path+'\\'+name for name in os.listdir(dir_path) if os.path.isfile(dir_path+'\\'+name) ]
# 取得路徑下的資料夾名稱與完整路徑
only_dir=[dir_path+'\\'+name for name in os.listdir(dir_path) if os.path.isdir(dir_path+'\\'+name) ]
# 取的路徑下的 file 名稱
only_file2 = [ele for ele in dir_list if os.path.isfile(os.path.join(dir_path,ele))]
# 取的路徑下的 dir 名稱
only_dir2 = [ele for ele in dir_list if os.path.isdir(os.path.join(dir_path,ele))]

print(dir_list)
print('')
print(only_file)
print(only_dir)
print('')
print(only_file2)
print(only_dir2)

Result:

['dir1', 'dir2', 'file1.txt', 'file2.txt']

['E:\\download\\tmp\\test\\file1.txt', 'E:\\download\\tmp\\test\\file2.txt']
['E:\\download\\tmp\\test\\dir1', 'E:\\download\\tmp\\test\\dir2']

[ 'file1.txt', 'file2.txt']
['dir1', 'dir2']
將深層的檔案路徑加到list中

Code:

import os
dir_path = r'E:\download\tmp2'
file_list=[]
for dirPath, dirNames, fileNames in os.walk(dir_path):
    for f in fileNames:
        file_list.append(os.path.join(dirPath, f))
print(file_list)

Result:

['E:\\download\\tmp2\\2017\\1\\1\\20170101_1.txt',
 'E:\\download\\tmp2\\2017\\1\\1\\20170101_2.txt',
 'E:\\download\\tmp2\\2017\\1\\2\\20170102_1.txt',
 'E:\\download\\tmp2\\2017\\1\\2\\20170102_2.txt']

2017年7月7日 星期五

Python - def mkdir - How can I create a directory if it does not exist - 使用python建立資料夾

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
System version :Windows 10
# create dir
def mkdir(path):
    import os
    if  os.path.exists(path) == False:
        os.makedirs(path)
        print('mkdir:',path)
    elif  os.path.exists(path) == True:
        print('dir already exist:',path)
mkdir(r'E:\download\tmp\test123')
執行結果:
mkdir: E:\download\tmp\test123 or dir already exist: E:\download\tmp\test123