Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | os.path.samestat() method
Next article icon

Python | os.path.sameopenfile() method

Last Updated : 10 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.
os.path.sameopenfile() method in Python is used to check whether the given file descriptors refer to the same file or not. 
A file descriptor is small integer value that corresponds to a file that has been opened by the current process. 
A File descriptor indicates a resource and act as handles to perform various lower level I/O operations like read, write, send etc. 
For Example: Standard input is usually file descriptor with value 0, standard output is usually file descriptor with value 1 and standard error is usually file descriptor with value 2. Further files opened by the current process will get the value 3, 4, 5 an so on.
 

Syntax: os.path.sameopenfile(fd1, fd2)
Parameter: 
fd1: A file descriptor. 
fd2: A file descriptor.
Return Type: This method returns a Boolean value of class bool. This method returns True if both file descriptor fd1 and fd2 refer to the same file otherwise returns false. 
 


Code : Use of os.path.sameopenfile() method to check if the given file descriptors refer to the same file or not. 
 

Python3
# Python program to explain os.path.sameopenfile() method     # importing os module  import os  # Path path = "/home / ihritik / Desktop / file1.txt"   # open the file represented by # the above given path and get # the file descriptor associated # with it using os.open() method fd1 = os.open(path, os.O_RDONLY)   # open the file represented by # the above given path and get # the file object corresponding  # to the opened file  # using open() method File = open(path, mode ='r')   # Get the file descriptor # associated with the  # file object 'File'  fd2 = File.fileno()   # check whether the file descriptor # fd1 and fd2 refer to same  # file or not sameFile = os.path.sameopenfile(fd1, fd2)  # Print the result print(sameFile)   # Path path2 = "/home / ihritik / Documents / sample.txt"   # open the file represented by # the above given path and get # the file descriptor associated # with it using os.open() method fd3 = os.open(path2, os.O_RDONLY)   # check whether the file descriptor # fd1 and fd3 refer to same  # file or not sameFile = os.path.sameopenfile(fd1, fd3)  # Print the result print(sameFile)   # close file descriptors close(fd1) close(fd2) close(fd3) 

Output: 
True False

 

Reference: https://docs.python.org/3/library/os.path.html
 


Next Article
Python | os.path.samestat() method

I

ihritik
Improve
Article Tags :
  • Python
  • python-os-module
  • Python OS-path-module
Practice Tags :
  • python

Similar Reads

  • Python | os.path.getctime() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man
    2 min read
  • Python | os.path.size() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependentthe functionality. os.path module is a submodule of the OS module in Python used for common path
    2 min read
  • Python | os.path.isabs() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man
    2 min read
  • Python os.path.isfile() method
    The os.path.isfile() method in Python is used to check if a path refers to a regular file (not a directory or any other type of file system object). This method can be particularly useful when you need to validate file paths before performing file operations like reading, writing, or modifying files
    2 min read
  • os.path.isdir() method - Python
    os.path.isdir() method in Python checks whether the specified path is a directory or not. This method follows the symbolic link, which means if the specified path is a symbolic link pointing to an existing directory then the method will return True. Example: Using os.path.isdir() methodThis code dem
    2 min read
  • Python | os.path.islink() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man
    2 min read
  • Python | os.path.ismount() method
    os.path module is sub module of Python OS Module in Python used for common path name manipulation. os.path.ismount() method in Python is used to check whether the given path is a mount point or not. A mount point is a point in a file system where a different file system has been mounted. os.path.ism
    2 min read
  • Python | os.path.join() method
    The os.path.join() method is a function in the os module that joins one or more path components intelligently. It constructs a full path by concatenating various components while automatically inserting the appropriate path separator (/ for Unix-based systems and \ for Windows). Example [GFGTABS] Py
    4 min read
  • Python | os.path.normcase() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man
    2 min read
  • Python | os.path.normpath() method
    OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences