Traditionally, Python has represented file paths using regular text strings. However, since paths are not strings, important functionality is spread all around the standard library, including libraries like os, glob, and shutil.
The pathlib module was introduced in Python 3.4 to deal with these challenges. It gathers the necessary functionality in one place and makes it available through methods and properties on an easy-to-use Path object.
In this library, all you really need to know about is the pathlib.Path class. The best way to construct a path is to join the parts of the path using the special operator /.
You can use Path.cwd() or Path('.') to refer to your currently working directory.
Traditionally, the way to read or write a file in Python has been to use the built-in open() function. When you're using pandas, you can read it directly by putting the Path object in the read_csv or read_table.
The different parts of a path are conveniently available as properties. Basic examples include: .name, .parent, .stem, .suffix, .anchor . Note that, the pathlib.Path is represented by either a WindowsPath or a PosixPath.
Using .iterdir() you can get all the files in a folder. By list comprehension, you can convert this into a list object.
Path.exists()
Checks if a path exists or not. Returns boolean value.
Path.glob()
Globs and yields all file paths matching a specific pattern. Glob patterns specify sets of filenames with wildcard characters. For example, the *.txt represents all files with names ending in .txt. The other common wildcard is the question mark (?), which stands for one character.
Path.rglob()
This is like Path.glob method but matches the file pattern recursively.
You can use Path.cwd() or Path('.') to refer to your currently working directory.
Path.mkdir()
Creates a new directory at this given path. Parameters:
mode:(str) Posix permissions (mimicking the POSIX mkdir -p command)
parents:(boolean) If parents is True, any missing parents of this path are created as needed. Otherwise, if the parent is absent, FileNotFoundError is raised.
exist_ok: (boolean) If False, FileExistsError is raised if the target directory already exists. If True, FileExistsError is ignored.
Path.rename(target)
Renames this file or directory to the given target and returns a new Path instance pointing to target. This will raise FileNotFoundError if the file is not found.
Path.replace(target)
Replaces a file or directory to the given target. Returns the new path instance.
Path.rmdir()
Removes a path pointing to a file or directory. The directory must be empty, otherwise, OSError is raised.
Try to do the following:
Make a new folder called folder_new.
Combine data1.csv and data2.csv (using append()) then save the new csv (name it data.csv) in folder_new.
Combine all the text file in folder 1 and 2 that start with 'text' then save it in a new csv (name it text_combined.csv) in folder_new.
Please erase folder_new manually before submitting the code.