Python Forum
File system navigation. Changing directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File system navigation. Changing directory
#1
Using Python 2.7. I want to navigate filesystem on a raspberry pi Linux box with pygame module. I am developing on a windows box in eclipse because it is quicker. Using a gamepad, and the XYAB buttons, I want to move around the filesystem and operate on the files. I am having trouble changing directory in order to do this. I understand there is a different paradigm in Linux as opposed to windows but I am finding this confusing.

import pygame, sys, os,subprocess
from subprocess import call
from pygame.locals import * # This module contains various constants used by Pygame


global p    #position in file_list
global n    #counter
global s    #size of file_list
global folder_cwd
global file_list


n=0
s=4
p=0

os.chdir("d:\\test\sub_1")
folder_cwd=os.getcwd()
file_list=os.listdir(os.getcwd())
s=len(file_list)


A = 0
B = 1
X = 2
Y = 3
     
def finish():
    ''' Self explanatory '''
    pygame.quit()
    sys.exit(0)
    
def input(events):
    ''' Function to handle events, particularly quitting the program '''
    global file_name
    global p    #position in the file_list
    global n    #counter
    global file_list
    global folder_cwd
    file_list=os.listdir(os.getcwd())
    s=len(file_list)
    
    for event in events:
        if event.type == JOYBUTTONDOWN: #using joystick buttons to navigate the filesystem
            button=event.button
            if(button==6):
                finish()    #quit()
            #print button
            folder_cwd=os.getcwd()
            file_list=os.listdir(os.getcwd())
            s=len(file_list)
            
            if(button==Y):  #cycling through the files in the current directoty
                n+=1    #moving to next file
                p=n%s
                file_name=file_list[p]
                if (os.path.isdir(file_name)):
                    print "This is a folder so we can change to it using the B button : ",file_name   
                
                print "This is a file : ",file_name
                    
                 
            if(button==A):  #cycling through the files in the current directoty
                n+=-1
                p=abs(n%s)
                file_name=file_list[p] 
                if (os.path.isdir(file_name)):
                    print "This is a folder so we can change to it using the B button : ",file_name
                print "This is a file : ",file_name
                
                    
                                  
            if(button==B):
                try:
                    new_folder=os.path.abspath(file_name)
                    print "os.path.basename : ",os.path.basename(file_name)
                    print "os.path.abspath : ",os.path.abspath(file_name)
                    print "os.getcwd() BEFORE CHANGE IS : ",os.getcwd()
                    
                    if os.path.isdir(new_folder):
                        print new_folder,"is a folder"
                        os.chdir(new_folder)
                        print "folder_getcwd IS : ",os.getcwd()
                        print "file_list is :",os.listdir(os.getcwd())  #DOESN'T PRINT THE FILE LIST
                    else:
                        print "Path doent exist"
                    
                except:
                    pass
                                
          
                    
pygame.init()
  
# Enable joystick support
pygame.joystick.init()

# Detect if joystick is available
joysticks = pygame.joystick.get_count()
for i in range(joysticks):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()

# The game loop
while True:
    input(pygame.event.get())
    
On pressing button B with a folder as the current 'file_name' there is no output from the line
print "file_list is :",os.listdir(os.getcwd())
I understand python 3 makes this kind of thing easier.
Reply
#2
I run Linux natively, I ran your code via python2 game.py and discovered that your error is on line 17. On Linux there is no such thing as "d:\test\sub_1" even if I were to create that file/folder structure. We dont have drive letters, and our path names are delimited by "/" forwards slash. if you change it to os.chdir("/") it seems to work, when I pressed Y it would cycle through the folders/files, when it got to a folder I used B to get into it. Once I did this, I was nolonger able to list my files however so your code prob has a bug that I don't see.

Here is how our filesystem works:

1) There are no drive letters
2) The various hard disks installed under the system are mounted usualy to /mnt/something
3) We dont use backslash
4) / on Linux is simular to C:/ on windows
Reply
#3
In Linux, everything is a file.

All Linux devices are listed in /dev/.

All partitions are usually mounted in /madia/$USER/ or /media/. The old mount directory was /mnt/ which is still there. These directories are called mount points. When a device or a partition is mounted, you browse the files in these specific directories. There are some special mount points such as /proc/ or /run/:
http://www.h-online.com/open/news/item/L...19006.html
http://www.tldp.org/LDP/Linux-Filesystem.../proc.html
https://www.linux.com/news/discover-poss...-directory

See the info about the /proc directory and you will understand the power of this system and how amazing Linux is.

You will get a better understanding of the file system if you look at this: http://www.tldp.org/LDP/intro-linux/html...03_01.html
For example.

There are some differences though. All of this depends on the Linux distribution. Or the partition file system itself. Like the btrfs which can span across multiple devices. See this: http://www.zdnet.com/article/btrfs-hands...le-system/
It will give you just an idea how a file system should work.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Thanks to everyone who replied. Problem solved.
I needed
file_list=os.listdir(os.getcwd())  
s=len(file_list)

after os.chdir in button B.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,496 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,085 Jun-27-2023, 01:17 PM
Last Post: diver999
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 928 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,071 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,495 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 985 Oct-07-2022, 02:01 PM
Last Post: Nasir
  Changing the initial worksheet name in an MS Excel file azizrasul 3 908 Oct-02-2022, 07:56 PM
Last Post: azizrasul
  Changing file location azizrasul 6 1,237 Sep-28-2022, 01:01 AM
Last Post: azizrasul
  Failed to execute child process (No such file or directory) uriel 1 1,616 Sep-15-2022, 03:48 PM
Last Post: Gribouillis
  Need Help: FileNotFoundError:[Errno 2] No such file or directory python202209 5 2,529 Sep-12-2022, 04:50 AM
Last Post: python202209

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020