0

I'm writing script to automate system administration tasks on my Linux system.

This involves compressing, moving, deleting, renaming and searching a lot of files. I learned doing these in Python by looking up examples on Internet and here on Stackoverflow.

The typical approach is to create a command string and pass it to the OS to have it executed.

print Popen("cat /home/kshitiz/myfile", stdout=PIPE).stdout.read()

The directory paths are handled as strings.

Now consider following example:

We have a program that asks user for a dir and a file and creates a path. If a user enters /home/kshitiz and myfile the path becomes /home/kshitizmyfile. In Python I must do jugglery with strings to deal with this. In Java I can simply do: new File(parentDir, filename)

Since Python is supposed to be better at OS administration it surprises me that it doesn't have object oriented abstractions over the file system.

What are the other approaches in Python to deal with the file system?

1 Answer 1

1

You are looking for the os.path module:

path = os.path.join(parentDir, filename)
Sign up to request clarification or add additional context in comments.

3 Comments

Wow that was easy. :)
Are there modules that would let me deal with running processes and allow killing and searching?
There is psutil, or just use the os and subprocess modules directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.