Using pathlib, is there a simple solution to change a file extension with two suffixes like ".tar.gz" to a simple suffix like ".tgz".
Currently I tried:
import pathlib
src = pathlib.Path("path/to/archive.tar.gz")
dst = src.with_suffix("").with_suffix(".tgz")
print(dst)
I get:
path/to/archive.tgz
This question is related but not identical to Changing file extension in Python
is using path lib a requirement?
if not, the os module would work just fine:
import os
path_location = "/path/to/folder"
filename = "filename.extension"
newname = '.'.join([filename.split('.')[0], 'tgz'])
os.rename(os.path.join(path_location, filename), os.path.join(path_location, newname))
EDIT:
found this on the pathlib docs:
PurePath.with_suffix(suffix)¶
Return a new path with the suffix changed. If the original path doesn’t have a suffix, the new suffix is appended instead. If the suffix is an empty string, the original suffix is removed:
>>>
>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
>>> p.with_suffix('.bz2')
PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
>>> p = PureWindowsPath('README')
>>> p.with_suffix('.txt')
PureWindowsPath('README.txt')
>>> p = PureWindowsPath('README.txt')
>>> p.with_suffix('')
PureWindowsPath('README')
EDIT 2:
from pathlib import Path
p = Path('path/to/tar.gz')
new_ext = "tgz"
filename = p.stem
p.rename(Path(p.parent, "{0}.{1}".format(filename, new_ext)))
You could rename it.
import os
old_file = os.path.join("directory_where_file", "a.tar.gz")
new_file = os.path.join("directory_where_file", "b.tgz")
os.rename(old_file, new_file)
from pathlib import Path
p = Path('path/to/tar.gz')
name_without_ext = p.stem
p.rename(Path(p.parent, name_without_ext + '.' + new_ext))
Related
I need help renaming .jpg files in my folder with the same prefix, 'cat_'. for example, 070.jpg should be renamed cat_070.jpg.
the files are located within the Cat folder:
from pathlib import Path
p = Path('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\')
so I dont quite see how to do it? the below is wrong because it does not 'look into' the files in this directory.
p.rename(Path(p.parent, 'cat_' + p.suffix))
I have also unsuccessfully tried this:
import os
from os import rename
from os import listdir
# Get path
cwd = "C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat"
# Get all files in dir
onlyfiles = [f for f in listdir(cwd) if isfile(join(cwd, f))]
for file in onlyfiles:
# Get the current format
if file[-4:]==(".jpg"):
s = file[1]
# Change format and get new filename
s[1] = 'cat'
s = '_'.join(s)
# Rename file
os.rename(file, s)
print(f"Renamed {file} to {s}")
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\'
how can I do it? sorry I'm really a beginner here.
How about:
from pathlib import Path
img_dir = Path('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\') # path to folder with images
for img_path in img_dir.glob('*.jpg'): # iterate over all .jpg images in img_dir
new_name = f'cat_{img_path.stem}{img_path.suffix}' # or directly: f'cat_{img_path.name}'
img_path.rename(img_dir / new_name)
print(f'Renamed `{img_path.name}` to `{new_name}`')
pathlib also supports renaming files, so the os module is not even needed here.
use pathlib. Path. iterdir() to rename all files in a directory
1) for path in pathlib. Path("a_directory"). iterdir():
2) if path. is_file():
3) old_name = path. stem. original filename.
4) old_extension = path. suffix. original file extension.
5) directory = path. parent. ...
6) new_name = "text" + old_name + old_extension.
7) path. rename(pathlib.
I am trying to find files with .desktop extension in a specific directory in Python3. I tried the code snippet below but it didn't work as I wanted. I want it to be a single string value.
import os, fnmatch
desktopfile = configparser.ConfigParser ()
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
script_tmp_dir = "/tmp/appiload/appinstall" # Geçici dizin (dosyalar burada ayıklanıyor)
desktopfilea=f"{script_tmp_dir}/squashfs-root/{str(find ('*.desktop', f'{script_tmp_dir}/squashfs-root/')}"
print(desktopfilea)
desktopfile.items()
Result:
/tmp/appiload/appinstall/squashfs-root/['/tmp/appiload/appinstall/squashfs-root/helloworld.desktop']
Use glob.glob instead of writing a function to do this job.
import os, glob
desktopfile = configparser.ConfigParser ()
script_tmp_dir = "/tmp/appiload/appinstall" # Geçici dizin (dosyalar burada ayıklanıyor)
desktopfilea = glob.glob(f'{script_tmp_dir}/squashfs-root/*.desktop')
# desktopfilea = " ".join(desktopfilea) # Join them in one string, using space as seperator
print(str(desktopfilea))
desktopfile.items()
I don't exactly understand what do you mean but I made a simple program that will print all the files with the .desktop extension and save them to 2 files: applications.json in an array and applications.txt just written one after another.
I also have 2 versions of the program, one that will only print and save only the file names with extensions and other one that will print and save the whole path.
File names only:
import os
import json
strApplications = ""
applications = []
for file in os.listdir(os.path.dirname(os.path.realpath(__file__))):
if file.endswith(".desktop"):
applications.append(file)
with open("applications.json", "w") as f:
json.dump(applications, f)
strApplications = strApplications + file + "\n"
with open("applications.txt", "w") as f:
f.write(strApplications)
print(strApplications)
Full file path:
import os
import json
cwd = os.getcwd()
files = [cwd + "\\" + f for f in os.listdir(cwd) if f.endswith(".desktop")]
with open("applications.json", "w") as f:
json.dump(files, f)
with open("applications.txt", "w") as f:
f.write("\n".join(files))
print("\n".join(files))
I tried to name the file or rename a file in python, but it is showing a invalid syntax at the end of the path, before ',r'
os.rename(r 'D:\\Stackoverflow\\SC.png' ,r 'D:\\Stackoverflow\\Screen'+str(localtime)+'.png')
Try this:
from datetime import datetime
import os
src = "/tmp/test.txt"
dst = "/tmp/python_" + str(datetime.now().strftime('%Y-%m-%d-%H_%M_%S')) +".inf"
os.rename(src, dst)
if you have 4 files named SampleA.txt, SampleB.txt Samble25.txt and SampleA21.txt.
And that you have a tab-delimited txt file where one column has the original pattern (SampleA, SampleB, Sample25, SampleA21) and another column with corresponding new pattern (Community1, Community2, Community3, Community4),
is there a way to change the files title from the original pattern (first column) to the new patter (second column)?
just had a quick hack in Python, maybe something like this would be useful:
#!/bin/env python3
from sys import argv
from pathlib import Path
import csv
with open('rename-pats.txt') as fd:
inp = csv.reader(fd, delimiter='\t')
patterns = []
for src, dst in inp:
patterns.append((src, dst))
for path in argv[1:]:
path = Path(path)
name = path.name
for src, dst in patterns:
name = name.replace(src, dst)
if path.name != name:
path.rename(path.with_name(name))
relies on a file called rename-pats.txt containing something like:
SampleA Community1
SampleB Community2
Sample25 Community3
SampleA21 Community4
which would then be run as:
python3 mmv.py *.txt
Relatively new to python ( not using it everyday ). However I am trying to simplify some things. I basically have Keys which have long names however a subset of the key ( or file name ) has the same sequence of the associated folder.{excuse the indentation, it is properly indented.} I.E
file1 would be: 101010-CDFGH-8271.dat and folder is CDFGH-82
file2 would be: 101010-QWERT-7425.dat and folder is QWERT-74
import os
import glob
import shutil
files = os.listdir("files/location")
dest_1 = os.listdir("dest/location")
for f in files:
file = f[10:21]
for d in dest_1:
dire = d
if file == dire:
shutil.move(file, dest_1)
The code runs with no errors, however nothing moves. Look forward to your reply and chance to learn.
Sorry updated the format.
Try a variation of:
basedir = "dest/location"
for fname in os.listdir("files/location"):
dirname = os.path.join(basedir, fname[10:21])
if os.path.isdir(dirname):
path = os.path.join("files/location", fname)
shutil.move(path, dirname)