பைத்தானில் இயங்கும் கோப்பின் இருப்பிடம் (பாதை) பெறுதல்: __file__.

வணிக

பைத்தானில் இயங்கும் ஸ்கிரிப்ட் கோப்பின் இருப்பிடம் (பாதை) பெற, __file__ ஐப் பயன்படுத்தவும். இயங்கும் கோப்பின் இருப்பிடத்தின் அடிப்படையில் மற்ற கோப்புகளை ஏற்றுவதற்கு இது பயனுள்ளதாக இருக்கும்.

பைதான் 3.8 வரை, __file__ பைதான் கட்டளையை இயக்கும்போது குறிப்பிடப்பட்ட பாதையை வழங்குகிறது (அல்லது சில சூழல்களில் python3 கட்டளை). உறவினர் பாதை குறிப்பிடப்பட்டால், தொடர்புடைய பாதை திரும்பப் பெறப்படும்; ஒரு முழுமையான பாதை குறிப்பிடப்பட்டால், முழுமையான பாதை திரும்ப வழங்கப்படும்.

பைதான் 3.9 மற்றும் அதற்குப் பிறகு, இயக்க நேரத்தில் குறிப்பிடப்பட்ட பாதையைப் பொருட்படுத்தாமல் முழுமையான பாதை திரும்பப் பெறப்படுகிறது.

பின்வரும் உள்ளடக்கங்கள் விளக்கப்பட்டுள்ளன.

  • os.getcwd(),__file__
  • தற்போது இயங்கும் கோப்பின் கோப்பு பெயர் மற்றும் கோப்பகப் பெயரைப் பெறுங்கள்.
  • செயல்படுத்தப்படும் கோப்பின் முழுமையான பாதையைப் பெறுங்கள்.
  • தற்போது இயங்கும் கோப்பின் இருப்பிடத்தின் அடிப்படையில் மற்ற கோப்புகளைப் படிக்கிறது.
  • தற்போதைய கோப்பகத்தை செயல்படுத்தப்படும் கோப்பின் கோப்பகத்திற்கு நகர்த்தவும்.
  • இயக்க நேரத்தில் தற்போதைய கோப்பகத்தைப் பொருட்படுத்தாமல் அதே செயலாக்கத்தை செய்ய முடியும்.

தற்போதைய அடைவை (வேலை அடைவு) பெறுவது மற்றும் மாற்றுவது பற்றிய தகவலுக்கு பின்வரும் கட்டுரையைப் பார்க்கவும்.

ஜுபைட்டர் நோட்புக்கில் (.ipynb) __file__ பயன்படுத்த முடியாது என்பதை நினைவில் கொள்க.
.Ipynb அமைந்துள்ள கோப்பகம் ஜூபிட்டர் நோட்புக் தொடங்கப்பட்ட கோப்பகத்தைப் பொருட்படுத்தாமல் தற்போதைய கோப்பகமாக செயல்படுத்தப்படும்.
தற்போதைய கோப்பகத்தை மாற்ற குறியீட்டில் os.chdir () ஐப் பயன்படுத்த முடியும்.

os.getcwd () மற்றும் __file__.

விண்டோஸில், தற்போதைய கோப்பகத்தை சரிபார்க்க pwd க்கு பதிலாக dir கட்டளையைப் பயன்படுத்தலாம்.

pwd
# /Users/mbp/Documents/my-project/python-snippets/notebook

கீழேயுள்ள உள்ளடக்கங்களுடன் ஒரு பைதான் ஸ்கிரிப்ட் கோப்பை (file_path.py) உருவாக்கவும் (தரவு \ src).

import os

print('getcwd:      ', os.getcwd())
print('__file__:    ', __file__)

பைதான் கட்டளையை இயக்கவும் (அல்லது சில சூழல்களில் python3 கட்டளை) ஸ்கிரிப்ட் கோப்புக்கான பாதையைக் குறிப்பிடுகிறது.

python3 data/src/file_path.py
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook
# __file__:     data/src/file_path.py

தற்போதைய அடைவுக்கான முழுமையான பாதையை os.getcwd () உடன் பெறலாம். Python3 கட்டளையால் குறிப்பிடப்பட்ட பாதையைப் பெற நீங்கள் __file__ ஐப் பயன்படுத்தலாம்.

பைதான் 3.8 வரை, __file__ பைதான் (அல்லது பைதான் 3) கட்டளையில் குறிப்பிடப்பட்டுள்ள பாதையைக் கொண்டிருக்கும். மேலே உள்ள எடுத்துக்காட்டில், உறவினர் பாதை ஒப்பீட்டளவில் இருப்பதால் திருப்பித் தரப்படுகிறது, ஆனால் அது முழுமையானதாக இருந்தால் முழுமையான பாதை திரும்பும்.

pwd
# /Users/mbp/Documents/my-project/python-snippets/notebook

python3 /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook
# __file__:     /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py

பைதான் 3.9 மற்றும் பித்தன் (அல்லது பைதான் 3) கட்டளையில் குறிப்பிடப்பட்டுள்ள பாதையைப் பொருட்படுத்தாமல், முழுமையான பாதையை __file__ க்குத் திருப்பித் தருகிறது.

பின்வரும் எடுத்துக்காட்டில், பைதான் 3.7 இல் உள்ள அதே ஸ்கிரிப்ட் கோப்பில் (file_path.py) குறியீட்டைச் சேர்த்து மேலே உள்ள கோப்பகத்துடன் தொடர்புடையதாக இயக்குவோம்.

பைதான் 3.7 இல், முழுமையான பாதை பயன்படுத்தப்படுகிறது. முடிவுகள் இந்தப் பகுதியின் இறுதியில் காட்டப்படும்.

தற்போது இயங்கும் கோப்பின் கோப்பு பெயர் மற்றும் கோப்பகப் பெயரைப் பெறுங்கள்.

இயங்கும் கோப்பின் கோப்பு பெயர் மற்றும் அடைவுப் பெயரைப் பெற, நிலையான நூலகத்தின் os.path தொகுதியில் பின்வரும் செயல்பாட்டைப் பயன்படுத்தவும்.

  • os.path.basename()
  • os.path.dirname()
print('basename:    ', os.path.basename(__file__))
print('dirname:     ', os.path.dirname(__file__))

மரணதண்டனை முடிவு.

# basename:     file_path.py
# dirname:      data/src

செயல்படுத்தப்படும் கோப்பின் முழுமையான பாதையைப் பெறுங்கள்.

உறவினர் பாதை __file__ உடன் பெறப்பட்டால், அதை os.path.abspath () மூலம் முழுமையான பாதையாக மாற்றலாம். அடைவுகள் முழுமையான பாதைகளாகவும் பெறப்படலாம்.

print('abspath:     ', os.path.abspath(__file__))
print('abs dirname: ', os.path.dirname(os.path.abspath(__file__)))

மரணதண்டனை முடிவு.

# abspath:      /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py
# abs dirname:  /Users/mbp/Documents/my-project/python-snippets/notebook/data/src

Os.path.abspath () இல் ஒரு முழுமையான பாதை குறிப்பிடப்பட்டால், அது அப்படியே திருப்பித் தரப்படும். எனவே, __file__ ஒரு முழுமையான பாதையாக இருந்தால், பின்வருபவை பிழையை ஏற்படுத்தாது.

  • os.path.abspath(__file__)

தற்போது இயங்கும் கோப்பின் இருப்பிடத்தின் அடிப்படையில் மற்ற கோப்புகளைப் படிக்கிறது.

செயல்படுத்தப்படும் கோப்பின் இருப்பிடம் (பாதை) அடிப்படையில் நீங்கள் மற்ற கோப்புகளைப் படிக்க விரும்பினால், os.path.join () ஐப் பயன்படுத்தி பின்வரும் இரண்டு கோப்புகளில் சேரவும்.

  • செயல்படுத்தப்பட்ட கோப்பின் அடைவு
  • இயங்கும் கோப்பிலிருந்து படிக்க வேண்டிய கோப்புக்கான உறவினர் பாதை.

நீங்கள் இயக்கும் கோப்பின் அதே கோப்பகத்தில் ஒரு கோப்பைப் படிக்க விரும்பினால், கோப்பின் பெயரை இணைக்கவும்.

print('[set target path 1]')
target_path_1 = os.path.join(os.path.dirname(__file__), 'target_1.txt')

print('target_path_1: ', target_path_1)

print('read target file:')
with open(target_path_1) as f:
    print(f.read())

மரணதண்டனை முடிவு.

# [set target path 1]
# target_path_1:  data/src/target_1.txt
# read target file:
# !! This is "target_1.txt" !!

மேல் நிலை “. \” ஆல் குறிப்பிடப்படுகிறது. நீங்கள் அதை அப்படியே விட்டுவிடலாம், ஆனால் பாதையை இயல்பாக்க மற்றும் கூடுதல் “. \” மற்றும் பிற எழுத்துக்களை அகற்ற நீங்கள் os.path.normpath () ஐப் பயன்படுத்தலாம்.

print('[set target path 2]')
target_path_2 = os.path.join(os.path.dirname(__file__), '../dst/target_2.txt')

print('target_path_2: ', target_path_2)
print('normalize    : ', os.path.normpath(target_path_2))

print('read target file:')
with open(target_path_2) as f:
    print(f.read())

மரணதண்டனை முடிவு.

# [set target path 2]
# target_path_2:  data/src/../dst/target_2.txt
# normalize    :  data/dst/target_2.txt
# read target file:
# !! This is "target_2.txt" !!

தற்போதைய கோப்பகத்தை செயல்படுத்தப்படும் கோப்பின் கோப்பகத்திற்கு நகர்த்தவும்.

ஸ்கிரிப்டில் செயல்படுத்தப்படும் கோப்பின் கோப்பகத்திற்கு தற்போதைய கோப்பகத்தை நகர்த்த os.chdir () ஐப் பயன்படுத்தவும்.

இது os.getcwd () மூலம் நகர்த்தப்பட்டிருப்பதைக் காணலாம்.

print('[change directory]')
os.chdir(os.path.dirname(os.path.abspath(__file__)))
print('getcwd:      ', os.getcwd())

மரணதண்டனை முடிவு.

# [change directory]
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook/data/src

தற்போதைய அடைவு நகர்த்தப்பட்டவுடன், கோப்பைப் படிக்கும்போது அதை இயங்கும் கோப்பின் கோப்பகத்துடன் இணைக்க வேண்டிய அவசியமில்லை. இயங்கும் கோப்பின் கோப்பகத்துடன் தொடர்புடைய பாதையை நீங்கள் குறிப்பிடலாம்.

print('[set target path 1 (after chdir)]')
target_path_1 = 'target_1.txt'

print('target_path_1: ', target_path_1)

print('read target file:')
with open(target_path_1) as f:
    print(f.read())

print()
print('[set target path 2 (after chdir)]')
target_path_2 = '../dst/target_2.txt'

print('target_path_2: ', target_path_2)

print('read target file:')
with open(target_path_2) as f:
    print(f.read())

மரணதண்டனை முடிவு.

# [set target path 1 (after chdir)]
# target_path_1:  target_1.txt
# read target file:
# !! This is "target_1.txt" !!
# 
# [set target path 2 (after chdir)]
# target_path_2:  ../dst/target_2.txt
# read target file:
# !! This is "target_2.txt" !!

இயக்க நேரத்தில் தற்போதைய கோப்பகத்தைப் பொருட்படுத்தாமல் அதே செயலாக்கத்தை செய்ய முடியும்.

நாம் காட்டியபடி, பின்வரும் முறைகளில் ஒன்றைப் பயன்படுத்தி, இயக்க நேரத்தில் தற்போதைய கோப்பகத்திலிருந்து சுயாதீனமாக ஸ்கிரிப்ட் கோப்பின் இருப்பிடத்தின் அடிப்படையில் கோப்புகளை ஏற்ற முடியும்.

  • இயங்கும் கோப்பின் கோப்பகத்தையும், இயங்கும் கோப்பிலிருந்து ஓஎஸ்.பாத்.ஜெயின் () ஐப் பயன்படுத்தி படிக்க வேண்டிய கோப்புக்கான தொடர்புடைய பாதையையும் இணைக்கவும்.
  • தற்போதைய கோப்பகத்தை செயல்படுத்தப்படும் கோப்பின் கோப்பகத்திற்கு நகர்த்தவும்.

தற்போதைய கோப்பகத்தை நகர்த்துவது எளிது, ஆனால் நிச்சயமாக, அதற்குப் பிறகு நீங்கள் அதிக கோப்புகளைப் படிக்கவோ எழுதவோ விரும்பினால், தற்போதைய அடைவு நகர்த்தப்பட்டதை நீங்கள் கணக்கில் எடுத்துக்கொள்ள வேண்டும்.

முந்தைய எடுத்துக்காட்டுகளின் முடிவுகள் கீழே தொகுக்கப்பட்டுள்ளன.

pwd
# /Users/mbp/Documents/my-project/python-snippets/notebook

python3 data/src/file_path.py
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook
# __file__:     data/src/file_path.py
# basename:     file_path.py
# dirname:      data/src
# abspath:      /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py
# abs dirname:  /Users/mbp/Documents/my-project/python-snippets/notebook/data/src
# 
# [set target path 1]
# target_path_1:  data/src/target_1.txt
# read target file:
# !! This is "target_1.txt" !!
# 
# [set target path 2]
# target_path_2:  data/src/../dst/target_2.txt
# normalize    :  data/dst/target_2.txt
# read target file:
# !! This is "target_2.txt" !!
# 
# [change directory]
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook/data/src
# 
# [set target path 1 (after chdir)]
# target_path_1:  target_1.txt
# read target file:
# !! This is "target_1.txt" !!
# 
# [set target path 2 (after chdir)]
# target_path_2:  ../dst/target_2.txt
# read target file:
# !! This is "target_2.txt" !!

முழுமையான பாதையைக் குறிப்பிடுவதன் முடிவு பின்வருமாறு.

pwd
# /Users/mbp/Documents/my-project/python-snippets/notebook

python3 /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook
# __file__:     /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py
# basename:     file_path.py
# dirname:      /Users/mbp/Documents/my-project/python-snippets/notebook/data/src
# abspath:      /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py
# abs dirname:  /Users/mbp/Documents/my-project/python-snippets/notebook/data/src
# 
# [set target path 1]
# target_path_1:  /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/target_1.txt
# read target file:
# !! This is "target_1.txt" !!
# 
# [set target path 2]
# target_path_2:  /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/../dst/target_2.txt
# normalize    :  /Users/mbp/Documents/my-project/python-snippets/notebook/data/dst/target_2.txt
# read target file:
# !! This is "target_2.txt" !!
# 
# [change directory]
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook/data/src
# 
# [set target path 1 (after chdir)]
# target_path_1:  target_1.txt
# read target file:
# !! This is "target_1.txt" !!
# 
# [set target path 2 (after chdir)]
# target_path_2:  ../dst/target_2.txt
# read target file:
# !! This is "target_2.txt" !!

முனையத்தில் தற்போதைய கோப்பகத்தை நகர்த்தி, அதே ஸ்கிரிப்ட் கோப்பை செயல்படுத்துவதன் விளைவாக கீழே காட்டப்பட்டுள்ளது. ஒரே கோப்பை வேறு இடத்திலிருந்து செயல்படுத்தினாலும் படிக்க முடியும் என்பதை நீங்கள் பார்க்கலாம்.

cd data/src

pwd
# /Users/mbp/Documents/my-project/python-snippets/notebook/data/src

python3 file_path.py
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook/data/src
# __file__:     file_path.py
# basename:     file_path.py
# dirname:      
# abspath:      /Users/mbp/Documents/my-project/python-snippets/notebook/data/src/file_path.py
# abs dirname:  /Users/mbp/Documents/my-project/python-snippets/notebook/data/src
# 
# [set target path 1]
# target_path_1:  target_1.txt
# read target file:
# !! This is "target_1.txt" !!
# 
# [set target path 2]
# target_path_2:  ../dst/target_2.txt
# normalize    :  ../dst/target_2.txt
# read target file:
# !! This is "target_2.txt" !!
# 
# [change directory]
# getcwd:       /Users/mbp/Documents/my-project/python-snippets/notebook/data/src
# 
# [set target path 1 (after chdir)]
# target_path_1:  target_1.txt
# read target file:
# !! This is "target_1.txt" !!
# 
# [set target path 2 (after chdir)]
# target_path_2:  ../dst/target_2.txt
# read target file:
# !! This is "target_2.txt" !!

Copied title and URL