Rename a folder with source folder name matched by wildcard ("*") - python

I have a local folder named "abcd-1" and I want to do something like this:
import os
os.rename("abcd*", "abcd")
I know there's only one such folder so it's a valid operation, but it doesn't look like os.rename supports *. How can I solve it?

See glob
>>> import os, glob
>>> for f in glob.glob("abcd*"):
... os.rename(f, "abcd")
...
>>>
Check if there is only one result or use glob.glob("abcd*")[0] for first result.
Use os.path.isdir() to check whether it is a directory

You can use a combination of glob , os.path.isdir() function (to determine if it is a directory) , and then os.rename() to rename the actual file.
Example -
import glob
import os
import os.path
lst = glob.glob("abcd")
for element in lst:
if os.path.isdir(element):
os.rename(element,"abcd")

Use the glob module
eg
glob.glob("abcd*")
will return ["abcd-1"]
then you can rename the folder
You should probably use an assert statement to make sure theres only 1 result

Related

Python how to delete a directory that contains a specific word

Hey I want to delete folders in Windows which contain a specific string in their name.
For example I want all folders deleted with the String "Chrome"
And here existing folders:
Chrome_95.0.4638.69
Chrome_96.0.4664.45
I tried it with * but it seems like it works only if you want to delete all files with the same extensions:
import shutil
shutil.rmtree(r'./BR/Chrome*')
Best Regards
Christian
Something like that should work:
import os
import re
import shutil
for directory in os.listdir() :
if re.fullmatch('.*Chrome.*',directory):
shutil.rmtree(directory)

How to fix pattern, that I use to get list of files in folder with standard library glob?

I have the following files:
/tmp/test_glob/client.log.71.gz
/tmp/test_glob/client.log.63.gz
/tmp/test_glob/client.log.11
/tmp/test_glob/core_dump.log
/tmp/test_glob/client.log.32
/tmp/test_glob/dm.log
/tmp/test_glob/client.log
/tmp/test_glob/client.log.1
/tmp/test_glob/client.log.64.gz
I want to get all .log files, EXCEPT the files, that end with .gz.
The desired result should be the following:
/tmp/test_glob/client.log.11
/tmp/test_glob/core_dump.log
/tmp/test_glob/client.log.32
/tmp/test_glob/dm.log
/tmp/test_glob/client.log
/tmp/test_glob/client.log.1
I have written this simple code:
import glob
import os
glob_pattern = u'*.log*'
for log_path in glob.glob(os.path.join('/tmp/test_glob', glob_pattern)):
print('log_path: ', log_path)
but it returns all file from folder /tmp/test_glob/
I tried to modify this pattern like this:
glob_pattern = u'*.log.[0-9][0-9]'
but it returns only
/tmp/test_glob/client.log.11
/tmp/test_glob/client.log.32
How to fix this pattern ?
Using Pythex(a Python regex tester), the match string
glob_pattern = u'.*(\.log)(?!.*(gz)).*'
Worked well for your goal.
Try **/*.log!(*.gz)
Test using globster.xyz
That isn't a glob pattern. You don't want glob. You want to use the re module functions to filter the results of os.listdir.

How to insert strings and slashes in a path?

I'm trying to extract tar.gz files which are situated in diffent files named srm01, srm02 and srm03.
The file's name must be in input (a string) to run my code.
I'm trying to do something like this :
import tarfile
import glob
thirdBloc = 'srm01' #Then, that must be 'srm02', or 'srm03'
for f in glob.glob('C://Users//asediri//Downloads/srm/'+thirdBloc+'/'+'*.tar.gz'):
tar = tarfile.open(f)
tar.extractall('C://Users//asediri//Downloads/srm/'+thirdBloc)
I have this error message:
IOError: CRC check failed 0x182518 != 0x7a1780e1L
I want first to be sure that my code find the .tar.gz files. So I tried to just print my paths after glob:
thirdBloc = 'srm01' #Then, that must be 'srm02', or 'srm03'
for f in glob.glob('C://Users//asediri//Downloads/srm/'+thirdBloc+'/'+'*.tar.gz'):
print f
That gives :
C://Users//asediri//Downloads/srm/srm01\20160707000001-server.log.1.tar.gz
C://Users//asediri//Downloads/srm/srm01\20160707003501-server.log.1.tar.gz
The os.path.exists method tell me that my files doesn't exist.
print os.path.exists('C://Users//asediri//Downloads/srm/srm01\20160707000001-server.log.1.tar.gz')
That gives : False
Any way todo properly this work ? What's the best way to have first of all the right paths ?
In order to join paths you have to use os.path.join as follow:
import os
import tarfile
import glob
thirdBloc = 'srm01' #Then, that must be 'srm02', or 'srm03'
for f in glob.glob(os.path.join('C://Users//asediri//Downloads/srm/', thirdBloc, '*.tar.gz'):
tar = tarfile.open(f)
tar.extractall(os.path.join('C://Users//asediri//Downloads/srm/', thirdBloc))
os.path.join will create the correct paths for your filesystem
f = os.path.join('C://Users//asediri//Downloads/srm/', thirdBloc, '*.tar.gz')
C://Users//asediri//Downloads/srm/srm01\20160707000001-server.log.1.tar.gz
Never use \ with python for filepaths, \201 is \x81 character. It results to this:
C://Users//asediri//Downloads/srm/srm01ΓΌ60707000001-server.log.1.tar.gz
this is why os.path.exists does not find it
Or use (r"C:\...")
I would suggest you do this:
import os
os.chdir("C:/Users/asediri/Downloads/srm/srm01")
for f in glob.glob(str(thirdBloc) + ".tar.gz"):
print f

how to get name of a file in directory using python

There is an mkv file in a folder named "export". What I want to do is to make a python script which fetches the file name from that export folder.
Let's say the folder is at "C:\Users\UserName\Desktop\New_folder\export".
How do I fetch the name?
I tried using this os.path.basename and os.path.splitext .. well.. didn't work out like I expected.
os.path implements some useful functions on pathnames. But it doesn't have access to the contents of the path. For that purpose, you can use os.listdir.
The following command will give you a list of the contents of the given path:
os.listdir("C:\Users\UserName\Desktop\New_folder\export")
Now, if you just want .mkv files you can use fnmatch(This module provides support for Unix shell-style wildcards) module to get your expected file names:
import fnmatch
import os
print([f for f in os.listdir("C:\Users\UserName\Desktop\New_folder\export") if fnmatch.fnmatch(f, '*.mkv')])
Also as #Padraic Cunningham mentioned as a more pythonic way for dealing with file names you can use glob module :
map(path.basename,glob.iglob(pth+"*.mkv"))
You can use glob:
from glob import glob
pth ="C:/Users/UserName/Desktop/New_folder/export/"
print(glob(pth+"*.mkv"))
path+"*.mkv" will match all the files ending with .mkv.
To just get the basenames you can use map or a list comp with iglob:
from glob import iglob
print(list(map(path.basename,iglob(pth+"*.mkv"))))
print([path.basename(f) for f in iglob(pth+"*.mkv")])
iglob returns an iterator so you don't build a list for no reason.
I assume you're basically asking how to list files in a given directory. What you want is:
import os
print os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")
If there's multiple files and you want the one(s) that have a .mkv end you could do:
import os
files = os.listdir("""C:\Users\UserName\Desktop\New_folder\export""")
mkv_files = [_ for _ in files if _[-4:] == ".mkv"]
print mkv_files
If you are searching for recursive folder search, this method will help you to get filename using os.walk, also you can get those file's path and directory using this below code.
import os, fnmatch
for path, dirs, files in os.walk(os.path.abspath(r"C:/Users/UserName/Desktop/New_folder/export/")):
for filename in fnmatch.filter(files, "*.mkv"):
print(filename)
You can use glob
import glob
for file in glob.glob('C:\Users\UserName\Desktop\New_folder\export\*.mkv'):
print(str(file).split('\')[-1])
This will list out all the files having extention .mkv as
file.mkv, file2.mkv and so on.
From os.walk you can read file paths as a list
files = [ file_path for _, _, file_path in os.walk(DIRECTORY_PATH)]
for file_name in files[0]: #note that it has list of lists
print(file_name)

Split filenames with python

I have files that I want only 'foo' and 'bar' left from split.
dn = "C:\\X\\Data\\"
files
f= C:\\X\\Data\\foo.txt
f= C:\\X\\Dats\\bar.txt
I have tried f.split(".",1)[0]
I thought since dn and .txt are pre-defined I could subtract, nope.
Split does not work for me.
How about using the proper path handling methods from os.path?
>>> f = 'C:\\X\\Data\\foo.txt'
>>> import os
>>> os.path.basename(f)
'foo.txt'
>>> os.path.dirname(f)
'C:\\X\\Data'
>>> os.path.splitext(f)
('C:\\X\\Data\\foo', '.txt')
>>> os.path.splitext(os.path.basename(f))
('foo', '.txt')
To deal with path and file names, it is best to use the built-in module os.path in Python. Please look at function dirname, basename and split in that module.
simple Example for your Help.
import os
from os import path
path_to_directory = "C:\\X\\Data"
for f in os.listdir(path_to_directory):
name , extension = path.splitext(f)
print(name)
Output
foo
bar
These two lines return a list of file names without extensions:
import os
[fname.rsplit('.', 1)[0] for fname in os.listdir("C:\\X\\Data\\")]
It seems you've left out some code. From what I can tell you're trying to split the contents of the file.
To fix your problem, you need to operate on a list of the files in the directory. That is what os.listdir does for you. I've also added a more sophisticated split. rsplit operates from the right, and will only split the first . it finds. Notice the 1 as the second argument.
another example:
f.split('\\')[-1].split('.')[0]
Using python3 and pathlib:
import pathlib
f = 'C:\\X\\Data\\foo.txt'
print(pathlib.PureWindowsPath(f).stem)
will print: 'foo'

Categories

Resources