I am struggling with the paths and directories to solve this problem. Basically, I have a long list of .lammps files in one directory. My goal is to copy each file and move it into its own folder (which is one directory back) where its folder name is the file name minus the .lammps. All of the folders are already made, I just can't seem to figure out moving them. The entire list of files is in the Files directory. The individual folders are in the ROTATED FILES directory. Here is what I have. Any tips greatly appreciated.
Here is a file example
n-optimized.new.10_10-90-10_10.Ni00Nj01.lammps
The folder for this file is then named
n-optimized.new.10_10-90-10_10.Ni00Nj01
import os
file_directory = os.chdir("C:\Py Practice\ROTATED FILES\Files")
files = os.listdir()
for file in files:
# get the file -.lammps string
name1 = file.split('.')[0:4]
name2 = ".".join(name1)
# get the path for the files new respective folder (back a directory and paste folder name)
file_folder = "C:\Py Practice\ROTATED FILES/" + name2
# Move
combined_path = os.path.join(file, file_folder)
I've tried shutil and figured path join might be easier.
First of all, the code you have here shouldn't work since you either have to escape backslashes or use a raw string. Secondly, rather than using
osfor file system operations, it's much better to learn how to usepathlib(also a core python module) which provides a more modern object-oriented approach to file operations.Using
pathlibandshutilyou can do something likeHere we're accessing different parts of file path using a convenient OOP structure. For example, if your file
fis located in'c:/foo/bar/boo.txt'thenf.nameis just the name of file:boo.txt,f.stemis the stem part of the file name (excluding the extension)boo,f.parentis its parent directory'c:/foo/bar/'etc.There's a really handy graphic of
pathlibPath objects here.The only inconvenience is that not all of core modules support
Pathobjects yet so forcopyfilewe just need to get the string representation by callingstron the object.And you don't even need to have target folders created beforehand, it's very easy to create the necessary folder structure as you go along: