This will return “C:/path/to” How to get a file’s extension
#File.path windows r code
Tweaking the code above, we can get the directory of a file like this: The above code will return “file.txt” How to get the directory name of a file Getting the base name of a file can be done using the basename function:
Running file.exists will return TRUE whether an existing file is a directory or not, whereas dir.exists will return TRUE if and only if the input value exists and is a directory. # alternatively, check if a folder exists with dir.existsĭir.exists("C:/path/to/file/some_folder") You can check if a file exists, using the file.exists function.įile.exists("C:/path/to/file/some_file.txt")įile.exists("C:/path/to/file/some_folder") How to check if a file or directory exists With unlink, we can delete the 100 text files we created above with file.create - also in just one line of code. Unlink("some_directory", recursive = TRUE) # delete a directory - must add recursive = TRUE To delete a directory, you have to add the parameter recursive = TRUE. Deleting a single file is as simple as passing the file’s name to this function. Getting the last modified time stamp is similar to above, except we use file.mtime:įile.mtime("C:/path/to/file/some_file.txt")įiles can be deleted with R using unlink. If you want to get just the created time stamp of a file, call file.ctime:įile.ctime("C:/path/to/file/some_file.txt") For instance, the code below will return the fields above (size, isdir, mode, mtime etc.) for the specific file, “some_file.csv”: exe => type of executable (or “no” if not an executable)įile.info is similar to fileSnapshot, except that it returns a single record of information corresponding to an input file.isdir => is file a directory? => TRUE or FALSE.You can accomplish this in a couple lines of code: For instance, suppose you have a list of CSV’s in a folder, and you want to produce a single data frame (provided they each have the same layout) from all the files. The above logic can be really useful if you want to read in all of the CSV files within a given directory. List.files(pattern = ".csv", recursive = TRUE) # list all CSV files recursively through each sub-folder For instance, the R code below will list all of the CSV files in a directory (similar to “ls | grep. List.files can also apply a filter internally to the files you want to list. List.files("C:/path/to/somewhere/else", full.names = TRUE, recursive = TRUE)
#File.path windows r full
We can set the parameter, full.names, to TRUE to get the full path names. running list.files(“C:/”, recursive = TRUE)).Īn additional point - running default list.files doesn’t list the full path names of the files. Note, adding the “recursive = TRUE” flag may cause the function to run for a longer period of time if the directory has a large number of sub-folders and files (e.g. List.files("C:/path/to/somewhere/else", recursive = TRUE) it doesn’t list the files within any sub-folder unless you tell it to do so. The simplest way of listing all the files in a directory with R is by calling list.files.Ĭalling list.files with no additional parameters will only list the files and folders directly within the directory - i.e. If the file copies successfully, the function will return TRUE - otherwise, it returns FALSE. With py, the first parameter is the name of the file to be copied the second is the destination folder that you want to copy the file to. Sapply(paste0("file", 1:100, ".txt"), file.create)Ĭopying a file can be done using py.įile.copy("source_file.txt", "destination_folder") For example, the one-liner below will create 100 empty text files: With this in mind, creating lots of files quickly is made easy. Similarly, creating a blank file can be done with file.create. If you don’t write out the full path of this new directory, it will get created into whatever the current working directory is i.e. You just need to replace “new_folder” with whatever name you choose. To change this directory, you can use the aptly named setwd function.Ī new folder, or directory, can be created in R using the dir.create function, like this: In R, you can figure this out with the getwd function. The working directory is the folder that any files you create or refer to without explicitly spelling out the full path fall within. How to get and change the current working directoryīefore working with files, it’s usually a good idea to first know what directory you’re working in. In this post we’re going to talk about using R to create, delete, move, and obtain information on files. Though Python is usually thought of over R for doing system administration tasks, R is actually quite useful in this regard.