I'm trying to build a basic terminal that performs basic operations in python. I have made all the main functions, but the cd function isn't working to change my current directory.
I suspect that the problem is in the way I store my current directory file. Perhaps I need to store it as variable instead of using function.
This is the code.
#####################################
# import modules.
# pwd - view the current folder function.
# ls - list files in a folder function.
# touch (filename) - create new empty file function.
# rm (filename) - delete a file function.
# cd - go to another folder function.
# cat (filename) - display the contents of a file function.
######################################
import os
import pathlib
from os.path import join
path = os.getcwd()
# DONE
def ls():
os.listdir(path)
print(os.listdir(path))
def pwd():
print(os.getcwd())
def touch(file_name):
fp = open(join(path, file_name), 'a')
fp.close()
def rm(file_name):
file = pathlib.Path(join(path, file_name))
file.unlink()
def cd(file_name):
os.chdir(join(path, file_name))
while True < 100:
dirName = input()
cmd = dirName.split(" ")[0]
if cmd == "ls":
ls()
elif cmd == "pwd":
pwd()
elif cmd == "cd":
file_name = dirName.split(" ")[1]
cd(file_name)
print(os.getcwd())
elif cmd == "touch":
file_name = dirName.split(" ")[1]
touch(file_name)
elif cmd == "rm":
file_name = dirName.split(" ")[1]
rm(file_name)
elif cmd == 'cd': #
file_name = dirName.split(" ")[1]
cd(file_name)
print(pwd(file_name))
else:
print("Command not found!")
I tired to change directory using the cd function in my custom terminal, but it's not working. It is expected that cd function to work correctly.
CodePudding user response:
It looks like you are storing the current working directory in the path variable when you import it at the beginning of your code. However, when you call os.chdir in your cd function, it changes the current working directory, but it doesn't update the path variable to reflect this change. As a result, when you call os.listdir in your ls function, it still lists the files in the old working directory instead of the new one.
One way to fix this is to update the path variable whenever you call os.chdir in the cd function. You can do this by assigning the result of os.chdir to path. This will update path to the new working directory, and the ls function will work as expected. Here is what the updated code might look like:
import os
import pathlib
from os.path import join
path = os.getcwd()
def ls():
os.listdir(path)
print(os.listdir(path))
def pwd():
print(os.getcwd())
def touch(file_name):
fp = open(join(path, file_name), 'a')
fp.close()
def rm(file_name):
file = pathlib.Path(join(path, file_name))
file.unlink()
def cd(file_name):
path = os.chdir(join(path, file_name)) # Update the path variable
while True < 100:
dirName = input()
cmd = dirName.split(" ")[0]
if cmd == "ls":
ls()
elif cmd == "pwd":
pwd()
elif cmd == "cd":
file_name = dirName.split(" ")[1]
cd(file_name)
print(os.getcwd())
elif cmd == "touch":
file_name = dirName.split(" ")[1]
touch(file_name)
elif cmd == "rm":
file_name = dirName.split(" ")[1]
rm(file_name)
elif cmd == 'cd': #
file_name = dirName.split(" ")[1]
cd(file_name)
print(pwd(file_name))
else:
print("Command not found!")
Another approach would be to use the os.getcwd function to get the current working directory inside each function instead of using the path variable. This way, the path variable won't be necessary, and you can remove it from your code. Here is an example of what this might look like:
import os
import pathlib
from os.path import join
def ls():
print(os.listdir(os.getcwd())) # Use os.getcwd() instead of path
def pwd():
print(os.getcwd())
def touch(file_name):
fp = open(join(os.getcwd(), file_name), 'a') # Use os.getcwd() instead of path
fp.close()
CodePudding user response:
There are a few issues with your cd function. First, you are using the global path variable to store the current working directory, but you are not updating this variable when calling cd. Second, the cd function does not return anything, so the print statement in the while loop does not have any effect.
Here is one way to fix these issues:
Instead of using a global variable to store the current working directory, use a local variable current_dir that is initialized to the current working directory. This variable should be updated whenever the cd function is called. Inside the cd function, change the current working directory using the os.chdir function and update the current_dir variable. In the while loop, call the pwd function after calling the cd function to display the new current working directory. Here is the updated code that implements these changes:
import os
import pathlib
from os.path import join
# DONE
def ls():
os.listdir(current_dir)
print(os.listdir(current_dir))
def pwd():
print(current_dir)
def touch(file_name):
fp = open(join(current_dir, file_name), 'a')
fp.close()
def rm(file_name):
file = pathlib.Path(join(current_dir, file_name))
file.unlink()
def cd(file_name):
os.chdir(join(current_dir, file_name))
current_dir = os.getcwd()
current_dir = os.getcwd()
while True < 100:
dirName = input()
cmd = dirName.split(" ")[0]
if cmd == "ls":
ls()
elif cmd == "pwd":
pwd()
elif cmd == "cd":
file_name = dirName.split(" ")[1]
cd(file_name)
pwd()
elif cmd == "touch":
file_name = dirName.split(" ")[1]
touch(file_name)
elif cmd == "rm":
file_name = dirName.split(" ")[1]
rm(file_name)
else:
print("Command not found!")
With these changes, the cd function should work as expected. You can further improve the code by adding error handling for invalid directory names and making the input parsing more robust.
CodePudding user response:
You have to change the path value to the updated path value.
Try this.
import os
import pathlib
from os.path import join
path = os.getcwd()
# DONE
def ls():
os.listdir(path)
print(os.listdir(path))
def pwd():
print(os.getcwd())
def touch(file_name):
fp = open(join(path, file_name), 'a')
fp.close()
def rm(file_name):
file = pathlib.Path(join(path, file_name))
file.unlink()
def cd(file_name):
global path
path = os.chdir(join(path, file_name))
while True < 100:
dirName = input()
cmd = dirName.split(" ")[0]
if cmd == "ls":
ls()
elif cmd == "pwd":
pwd()
elif cmd == "cd":
file_name = dirName.split(" ")[1]
cd(file_name)
print(os.getcwd())
elif cmd == "touch":
file_name = dirName.split(" ")[1]
touch(file_name)
elif cmd == "rm":
file_name = dirName.split(" ")[1]
rm(file_name)
elif cmd == 'cd': #
file_name = dirName.split(" ")[1]
cd(file_name)
print(pwd(file_name))
else:
print("Command not found!")