Learn R and Python Simultaneously (Part 1) : Play with Files and Directories.
By: Md Yunus Jamal
Tags: R, Python Programming

Working directories are very important for keeping project files organized. In simple language, a working directory is a folder created to save all your work such as written script, plots, summary, input-output files and so on. Best practice is to create a different directory to save each project you are working on. For this tutorial, i have created one folder on my desktop with the name “R-Python”. Please follow the steps and execute code as per screenshot.

Get Current Working Directory in R and Python:

In R, you can find out which directory by running the getwd() function. This function has no arguments.

Path for current working directory in R

In Python, we can get the current working directory using os.getcwd() method of the os module.

Path for current working directory in Python

Set Current Working Directory in R and Python:

I have to save all files and folders related to this tutorial in the directory “R-Python” which I have created on my desktop. Let’s set up through code:

In R, you can change your working directory by using function setwd() and specify the path to the desired folder. You have two options: “use double back-slash” or “use single forward-slash” to separate the path elements and set the current working directory.

Setting up current directory in R

In Python, you can change the current working directory by using os.chdir() method. The new path that you want to change into must be supplied as a string in method. Here also ,you have two options: “use double back-slash” or “use single forward-slash” to separate the path elements and set the current working directory.

Setting up current directory in Python

List the Files and Directories in R and Python:

In R, all files and subdirectories inside a directory can be executed by list.files() function. If no path specified, it will return a list of subdirectories and files from the current working directory.

List the files and sub folders from current working directory in R

List the files and sub folders from specific working directory in R

In Python, all files and sub-directories inside a directory can be retrieved using the os.listdir() method. If no path specified, it will return a list of subdirectories and files from the current working directory.

List the files and sub folders from current working directory in Python

List the files and sub folders from specific working directory in Python

Create Files and Directories in R and Python:

In R, a new folder or directory, can be created using the dir.create() function. Similarly, creating a blank file can be done with file.create() function.

Create files and Folders in R

In Python, you can make a new folder/directory using os.mkdir() method, and a new file with open() function by using append parameter “a”. If the full path is not specified then new folders and files are created in the current working directory.

Create files and Folders in Python

Rename the Files and Directories in R and Python:

In R, you will have to set the working directory of R to where the file resides, then execute file.rename() function to rename files and folders both.

Rename file and folder in R

In Python, you will have to set the working directory of Python, where the file resides, then execute os.rename() function to rename files and folders both.

Rename file and folder in Python

Remove files and folders in R and Python:

In R, you can use the function unlink() to remove files and folders both. Also function file.remove() can be used to remove existing files.

Remove file and folder in R

In Python, a file can be delete using os.remove() method. Similarly, the os.rmdir() method removes only an empty directory. In order to remove a non-empty directory , you can use the shutil.rmtree() method inside the shutil module.

Remove files and folders (empty and non-empty) in Python

This is my first article , hope you enjoyed it. I will extend this tutorial where you can learn R and Python simultaneously and feel comfortable in both programming language. If you have any question or query please let me know in the comments below.

Thanks for reading !