Several Modules in Python os
tutorial for pythonIn [ ]:
os
is a python module for using the operating system dependent functionality.
In [ ]:
In [ ]:
1 2 3 4 print (type (os.environ))print (os.environ)
In [ ]:
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.
In [ ]:
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!
In [ ]:
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.
In [ ]:
In [ ]:
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
In [ ]:
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!
In [ ]:
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
File Management