Linux File System - Access Directory in python

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
TheManInBlack
Level 1
Level 1
Posts: 1
Joined: Sat Mar 20, 2021 3:41 pm

Linux File System - Access Directory in python

Post by TheManInBlack »

Hello Guys! I am new to this forum and also new to the Linux operating system (which is an amazing OS by the way). I am also learning Python. I have got little problem regarding the Linux File System. If we open the Home folder on Desktop, we see on the left side of the window a TreeView. I have selected the First Option from the Devices, which is "File System" and opened as Root. Then I created two files: "/folder_x/folder_y" and created a file "/sample.txt". The problem is I cannot access this file within this directory using Python. If i use the "cd" command in Terminal like "cd /folder_x/folder_y" everything is alright but if I want to access it from Python using the open function, or even the os.open() method I get the error "No such file or directory". How do I access this directory correctly?

Terminal Command: "cd /folder_x/folder_y"
Python:
open("/folder_x/folder_y/sample.txt")
os.open("/folder_x/folder_y/sample.txt", os.RDWR)
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Moonstone Man
Level 16
Level 16
Posts: 6078
Joined: Mon Aug 27, 2012 10:17 pm

Re: Linux File System - Access Directory in python

Post by Moonstone Man »

TheManInBlack wrote: Sat Mar 20, 2021 4:01 pm ... I created two files: "/folder_x/folder_y" and created a file "/sample.txt". The problem is I cannot access this file within this directory using Python...
So, you created the path and file as root, but is your python code also root?

Don't answer that, it's rhetorical.

What we have here is your fundamental misunderstanding of the Linux file system, not a Linux Mint problem, and not a python problem.

First, you should never be off creating directories in the root or anywhere else as root unless you know exactly what you are doing. Second, you need to research and understand a litlle about the Linux file system, permissions, ownership, accessing a path, and so on. In the absence of that, you must confine your experiments to below your home directory or some other place where you have unfettered file system access.
mmphosis
Level 1
Level 1
Posts: 25
Joined: Sat Apr 11, 2020 11:22 pm

Re: Linux File System - Access Directory in python

Post by mmphosis »

Assuming the Directory and File exist and you have read permissions to /etc and /etc/shells in the Linux File System, here are some simple examples:

to Access a Directory in python

Code: Select all

import os
print(os.listdir(path='/etc'))
to Access a File in a Directory in python

Code: Select all

f=open("/etc/shells")
print(f.read())
f.close()
User avatar
tuxoneseven
Level 2
Level 2
Posts: 89
Joined: Mon Dec 14, 2020 8:08 am

Re: Linux File System - Access Directory in python

Post by tuxoneseven »

TheManInBlack wrote: Sat Mar 20, 2021 4:01 pm Hello Guys! I am new to this forum and also new to the Linux operating system (which is an amazing OS by the way). I am also learning Python. I have got little problem regarding the Linux File System. If we open the Home folder on Desktop, we see on the left side of the window a TreeView. I have selected the First Option from the Devices, which is "File System" and opened as Root. Then I created two files: "/folder_x/folder_y" and created a file "/sample.txt". The problem is I cannot access this file within this directory using Python. If i use the "cd" command in Terminal like "cd /folder_x/folder_y" everything is alright but if I want to access it from Python using the open function, or even the os.open() method I get the error "No such file or directory". How do I access this directory correctly?

Terminal Command: "cd /folder_x/folder_y"
Python:
open("/folder_x/folder_y/sample.txt")
os.open("/folder_x/folder_y/sample.txt", os.RDWR)
You can access files in your home directory (/home/you), but not in the root directory (/) if you are not root. But you can sometimes browse the files in the directories.

Code: Select all

$ python3
Python 3.9.2 (default, Feb 20 2021, 00:00:00) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system("ls ~/Documents")
>>> os.system("ls /")
bin   dev  home  lib64	     media  opt   root	sbin  sys  usr
boot  etc  lib	 lost+found  mnt    proc  run	srv   tmp  var
>>> 
You can read a file like this:

Code: Select all

>>> with open("/home/frank/test.txt") as data:
...     print(data.read())
... 
hello

>>> 
If you want to open a file anywhere on the file system, you need root access. You can do that with the command su or sudo. You don't need to cd into a directory, if you know the full path to the file
Active Python user - PyQt
Locked

Return to “Scripts & Bash”