Have to run glob.glob twice to work (Python3) - python

I am trying to grab all of the mp3 files in my Downloads directory (after procedurally downloading them) and move them to a new file. However, anytime I try to use glob to grab a list of the available .mp3 files, I have to glob twice for it to work properly (the first time it is running it returns an empty list). Does anyone know what I am doing wrong here?
import glob
import os
import shutil
newpath = r'localpath/MP3s'
if not os.path.exists(newpath):
os.makedirs(newpath)
list_of_files = glob.glob('localpath/Downloads/*.mp3')
for i in list_of_files:
shutil.move(i, newpath)

This turned out to be a timing issue. The files I was trying to access were still in the process of downloading, with is why the glob was returning empty. I inserted a time.sleep(5) before the glob, and it is now running smoothly.

May I suggest an alternate approach
from pathlib import Path
from shutil import move
music = Path("./soundtrack")
# you can include absolute paths too...
newMusic = Path("./newsoundtrack")
# makes new folder specified in newMusic
newMusic.mkdir(exist_ok=True)
# will select all
list_of_files = music.glob("*")
# u want to select only mp3's do:
# list_of_files = music.glob("*.mp3")
for file in list_of_files:
move(str(file), str(newMusic))

Related

Move files one by one to newly created directories for each file with Python 3

What I have is an initial directory with a file inside D:\BBS\file.x and multiple .txt files in the work directory D:\
What I am trying to do is to copy the folder BBS with its content and incrementing it's name by number, then copy/move each existing .txt file to the newly created directory to make it \BBS1, \BBS2, ..., BBSn (depends on number of the txt).
Visual example of the Before and After:
Initial view of the \WorkFolder
Desired view of the \WorkFolder
Right now I have reached only creating of a new directory and moving txt in it but all at once, not as I would like to. Here's my code:
from pathlib import Path
from shutil import copy
import shutil
import os
wkDir = Path.cwd()
src = wkDir.joinpath('BBS')
count = 0
for content in src.iterdir():
addname = src.name.split('_')[0]
out_folder = wkDir.joinpath(f'!{addname}')
out_folder.mkdir(exist_ok=True)
out_path = out_folder.joinpath(content.name)
copy(content, out_path)
files = os.listdir(wkDir)
for f in files:
if f.endswith(".txt"):
shutil.move(f, out_folder)
I kindly request for assistance with incrementing and copying files one by one to the newly created directory for each as mentioned.
Not much skills with python in general. Python3 OS Windows
Thanks in advance
Now, I understand what you want to accomplish. I think you can do it quite easily by only iterating over the text files and for each one you copy the BBS folder. After that you move the file you are currently at. In order to get the folder_num, you may be able to just access the file name's characters at the particular indexes (e.g. f[4:6]) if the name is always of the pattern TextXX.txt. If the prefix "Text" may vary, it is more stable to use regular expressions like in the following sample.
Also, the function shutil.copytree copies a directory with its children.
import re
import shutil
from pathlib import Path
wkDir = Path.cwd()
src = wkDir.joinpath('BBS')
for f in os.listdir(wkDir):
if f.endswith(".txt"):
folder_num = re.findall(r"\d+", f)[0]
target = wkDir.joinpath(f"{src.name}{folder_num}")
# copy BBS
shutil.copytree(src, target)
# move .txt file
shutil.move(f, target)

Find a file recursively using python

I have the below files located at some location in RHEL machine.
temp_file2.txt
temp_file3.txt
Looking for a python script to find above files recursively in all directories(I used a wild card, but it didn't work), and print a message if the file exists or not.
The below code snippet returns Nothing
import glob
for filename in glob.iglob('*/*.txt', recursive=True):
print(filename)
It returns the file name if it exists only in the current working directory
import glob
for filename in glob.iglob('.txt', recursive=True):
print(filename)
This approach seems to have worked for me, using python3.6
import glob
for f in glob.iglob('./**/*.yml', recursive=True):
print(f)
I was also able to use os.getcwd() + '/**/*.yml'. It appears there must be a directory definition at the start of the glob.

Removing folders from filepaths (python)

How could I take out the previous folder before the file from filepath using python? I mean I would like this:
C:\Projects\ProjectX\Stuff\File1\File1.jpg
to be
C:\Projects\ProjectX\Stuff\File1.jpg
Edit: And if the File is allready in "Stuff"-folder and not any subfolders after that, then I would like to leave the filepath like it was.
You can use shutil for transferring file from one place to another one
import shutil
shutil.move("C:\Projects\ProjectX\Stuff\File1\File1.jpg", "C:\Projects\ProjectX\Stuff\File1.jpg")
And you can simply follow answers from this post.
You can try this also:-
from pathlib import PureWindowsPath
import os
path = "your path here till file"
file_name = path.split('\\')[-1]
p = PureWindowsPath(path)
new_extension = os.path.join(p.parents[1],file_name)
print(new_extension)

How rename files in a directory using python?

The python script I am writing should be able to work in any folder I put in.
I have to find all the .avi files. If the file name ends with cropped.avi then I have to rename it and save it as .avi, without the word cropped.
import os
import glob
os.getcwd()
glob.glob('*.avi')
directory = glob.glob('*.avi')
for directory:
if i.endswith("cropped.avi"):
os.rename("cropped.avi",".avi")
I think my code is missing something and I don't know what to do!!
The for loop syntax was incorrect. This works:
import os
import glob
directory = glob.glob('*.avi')
for i in directory:
if i.endswith("cropped.avi"):
os.rename("cropped.avi",".avi")

Compile latex from python

I have made some python function for compiling passed string as pdf file using latex. The function works as expected and has been quite useful, therefore I look for ways to improve it.
The code which I have:
def generate_pdf(pdfname,table):
"""
Generates the pdf from string
"""
import subprocess
import os
f = open('cover.tex','w')
tex = standalone_latex(table)
f.write(tex)
f.close()
proc=subprocess.Popen(['pdflatex','cover.tex'])
subprocess.Popen(['pdflatex',tex])
proc.communicate()
os.unlink('cover.tex')
os.unlink('cover.log')
os.unlink('cover.aux')
os.rename('cover.pdf',pdfname)
The problem with the code is that it creates bunch of files named cover in the working directory which afterwards are removed.
How to avoid of creating unneeded files at the working directory?
Solution
def generate_pdf(pdfname,tex):
"""
Genertates the pdf from string
"""
import subprocess
import os
import tempfile
import shutil
current = os.getcwd()
temp = tempfile.mkdtemp()
os.chdir(temp)
f = open('cover.tex','w')
f.write(tex)
f.close()
proc=subprocess.Popen(['pdflatex','cover.tex'])
subprocess.Popen(['pdflatex',tex])
proc.communicate()
os.rename('cover.pdf',pdfname)
shutil.copy(pdfname,current)
shutil.rmtree(temp)
Use a temporary directory. Temporary directories are always writable and can be cleared by the operating system after a restart. tempfile library lets you create temporary files and directories in a secure way.
path_to_temporary_directory = tempfile.mkdtemp()
# work on the temporary directory
# ...
# move the necessary files to the destination
shutil.move(source, destination)
# delete the temporary directory (recommended)
shutil.rmtree(path_to_temporary_directory)

Categories

Resources