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)
Linux File System - Access Directory in python
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
Linux File System - Access Directory in python
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
-
- Level 16
- Posts: 6054
- Joined: Mon Aug 27, 2012 10:17 pm
Re: Linux File System - Access Directory in python
So, you created the path and file as root, but is your python code also root?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...
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.
Re: Linux File System - Access Directory in python
Assuming the Directory and File exist and you have read permissions to
to Access a Directory in python
to Access a File in a Directory in python
/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'))
Code: Select all
f=open("/etc/shells")
print(f.read())
f.close()
Re: Linux File System - Access Directory in python
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.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)
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
>>>
Code: Select all
>>> with open("/home/frank/test.txt") as data:
... print(data.read())
...
hello
>>>