Several Modules in Python
os
tutorial for python
os
is a python module for using the operating system dependent functionality.
1 2 3 4 print (type (os.environ))print (os.environ)
1 2 3 4 5 6 7 8 9 10 11 os.chdir("/GPFS/rhome/xiyuanyang/python_basic/Python-environment-modules-tutorial" )print (os.getcwd())print (os.getenv("BASE_URL" )) os.chdir(path="../" )print (os.getcwd())
File operating for os
You can use the os
library to perform basic file operations, including deleting and creating files and folders.
os.chdir()
: change the current directory.
os.makedirs()
: create a new directory.
os.remove()
& os.removedirs()
: remove the file or the directory.
os.rename()
: rename the directory.
open(file_name, 'a').close()
can be used to create a new file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 os.chdir("/GPFS/rhome/xiyuanyang/python_basic/Python-environment-modules-tutorial" )if os.path.exists("test" ): print ("test folder has been created!" )else : os.makedirs("test" ) file_name = "example.txt" if os.path.exists(file_name): print (f"{file_name} exists" )with open (file_name, 'w' ) as file: file.write("This is an example file." ) os.remove("example.txt" ) os.removedirs("test" )if not os.path.exists("2.txt" ): open ("2.txt" , 'a' ).close() if os.path.exists("1.txt" ): os.remove("1.txt" ) os.rename(src="2.txt" , dst="1.txt" )
Well, actually if you are familiar with bash commands, there is no need for you to learn these again!
1 2 3 4 5 os.system("echo 1" ) os.system("pwd" ) os.system("mkdir test" ) os.system("ls" ) os.system("rm -rf test" )
os.walk
and os.path
Very important! Most frequently used!
os.walk()
If you want to walk through all the files and directories in a certain directory, you can use os.walk()
to traverse.
1 2 3 4 5 6 for root, dirs, files in os.walk('.' ): print (f"Current directory: {root} " ) print (f"Subdirectories: {dirs} " ) print (f"Files: {files} " ) print ("-" * 40 )
os.path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 path_1 = "/GPFS/rhome/xiyuanyang/python_basic/" path_2 = "Python-environment-modules-tutorial/helloworld.txt" new_path = os.path.join(path_1, path_2)print (new_path)dir , file = os.path.split(new_path)print (dir , file)assert os.path.isdir(dir ) and os.path.isfile(file)open (new_path, "a" ).close()assert os.path.exists(new_path)print (f"size: {os.path.getsize(new_path)} " )print (new_path)print (os.path.basename(new_path))print (os.path.dirname(new_path))
Now, you can combine os.walk()
and os.path()
together to implement a small grep
command!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 def find_files (extension, search_path ): for root, dirs, files in os.walk(search_path): for file in files: print (f"root: {root} " ) print (f"dirs: {dirs} " ) print (f"file: {file} " ) print (f"files: {files} " ) if file.endswith(extension): full_path = os.path.join(root, file) print (f"Found file: {full_path} " ) print (f"Size: {os.path.getsize(full_path)} bytes" ) find_files('.ipynb' , '.' )
sys
Just see the PPT for several usage!
For me only, I often use sys.path.append()
command to add CWD path for python scripts.
See official tutorial for more information!
It will replace several commands which can be done in the command line environment.
File Management
Basic Usage
For the encoding part: encoding = 'utf-8'
.
1 2 3 with open ("test.txt" , "w" ) as file: file.write("Hello world" ) file.close()