How to read unicode filename in python? - python

I saw lot and lot of forums about unicode, utf-8 but unable to do this.
I am using Windows.
Let's have two folder:
E:\old
---- திருக்குறள்.txt
---- many more unicode named files
E:\new
----
Language : Tamil
Assume I want to move file to E:\new. I cannot access unicode filename properly.
What I Tried
import sys
import os
from shutil import copyfile
path = 'E:/old/'
for root, _, files in os.walk(ur''.join(path)):
files = [f for f in files]
copyfile(files[0].encode('utf-8').strip(),'E:/new/') //just for example
Error:
Traceback (most recent call last):
File "new.py", line 8, in <module>
copyfile(files[0].encode('utf-8').strip(),'E:/new/')
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: '\xe0\xae\xa4\xe0\xae\xbf\xe0\xae\xb0\xe0\xaf\x81\xe0\xae\x95\xe0\xaf\x8d\xe0\xae\x95\xe0\xaf\x81\xe0\xae\xb1\xe0\xae\xb3\xe0\xaf\x8d.txt'

In Windows use Unicode paths. Since you are using os.walk() you'll need to handle paths correctly to subdirectories, but you could just use shutil.copytree instead. If you don't need subdirectories, use os.listdir.
Here's something that works with os.walk:
import os
import shutil
for path,dirs,files in os.walk(u'old'):
for filename in files:
# build the source path
src = os.path.join(path,filename)
# build the destination path relative to the source path
dst = os.path.join('new',os.path.relpath(src,'old'))
try:
# ensure the destination directories and subdirectories exist.
os.makedirs(os.path.dirname(dst))
except FileExistsError:
pass
shutil.copyfile(src,dst)

Related

File identified but cannot be deleted - Python

So I'm trying to make the a program that deletes files which can be launched from the command lines. But when I run it, it fails and returns the following message: FileNotFoundError: [WinError 2] The system cannot find the file specified: 'test.txt'. Here's the code:
import sys
import os
num = int(sys.argv[1])
files = os.listdir(sys.argv[2])
for file in files[:num]:
print('Deleting '+file+'...')
os.remove(file)
The file is identified but it cannot be deleted.
You need to add the directory path back to the path:
import sys
import os
num = int(sys.argv[1])
files = os.listdir(sys.argv[2])
for file in files[:num]:
print('Deleting '+file+'...')
os.remove(os.path.join(sys.argv[2], file))
os.listdir will only return the basename of the file, whereas you'll need a relative or full path

Error while renaming files with os.rename() in Python

import os
for filename in os.listdir("C:/Users/Awesome/Music"):
if filename.endswith("lyrics.mp3"):
os.rename(filename,filename[0 : len(filename)-11]+".mp3")
The code above returns the error
File "c:/python/lyrics-pop.py", line 6, in <module>
os.rename(filename,filename[0 : len(filename)-11]+".mp3")
FileNotFoundError: [WinError 2] The system cannot find the file specified: '2 Chainz - Bigger Than You (feat Drake Quavo) lyrics.mp3' -> '2 Chainz - Bigger Than You (feat Drake Quavo).mp3'
"""
I have made sure that no other program is accessing the .mp3 files and removed the readonly attribute. What could be causing this?
Probably, the issue is that you are passing relative path to os.rename, add dir to file path, like this:
import os
dir = "C:/Users/Awesome/Music"
for filename in os.listdir(dir):
if filename.endswith("lyrics.mp3"):
os.rename(os.path.join(dir,filename),os.path.join(dir,filename[0 : len(filename)-11])+".mp3")
This is because python can not find the file from where this program is running, since full path is not given.
You can do it as follows:
import os
filedir = "C:/Users/Awesome/Music"
for filename in os.listdir(filedir):
if filename.endswith("lyrics.mp3"):
filepath = os.path.join(filedir, filename)
new_file = os.path.join(filedir, filename[0 : len(filename)-11]+".mp3")
os.rename(filepath, new_file)
As suggested in the comments, the problem seems to be the relative path of the files.
You can use glob, which will give you the full path, i.e.:
from glob import glob
from os import rename
for f in glob("C:/Users/Awesome/Music/*lyrics.mp3"):
rename(f, f[0 : len(f)-11]+".mp3")

Can't resolve error while moving files from one directory to another directory with Python

I have a question that
"Create a program that read the names of files in directory 'Task2' the names are in format UmSn where m=1 to 40 and n=1 to 40 separate the files into different directories based on m like U1,U2,U3......U40."
Hints: use 'os' module for reading directories and filenames.
I tried to solve it but can't.
Here is my code.
import shutil
import os,fnmatch
os.chdir("D:/MCS 2/MCS4/SL/Task2")
for i in range(1,41):
os.mkdir("U"+str(i))
files = os.listdir()
pattern = "*.TXT"
for i in range(1,41):
for f in files:
if f.startswith("U"+str(i)) and fnmatch.fnmatch(f, pattern):
shutil.move(f,("U"+str(i)))
I tried a lot but can't resolve this error.
Traceback (most recent call last):
File "C:\Users\kaleemi\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 557, in move
os.rename(src, real_dst)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'U10S1.TXT' -> 'U10\\U10S1.TXT'
Files start withU1 T0 U9 moves successfully but generate error while moving U10S1.TXT.
Hence the file also U10S1.TXTexist in directory.
Please help me to find where I am doing wrong in my code.
Perhaps you can try making sure you provide the absolute path instead with os.path.abspath():
from os.path import abspath
...
shutil.move(abspath(f),("U"+str(i)))

Unable to copy or move a file using python shutil

import shutil
import os
source = os.listdir("report")
destination = "archieved"
for files in source:
if files.endswith(".json"):
shutil.copy(files, destination)
I have the file named, 'a.json' inside the report folder report/a.json; however, the file is not moved/copied to the target folder
Console Error:
*** FileNotFoundError: [Errno 2] No such file or directory: 'a.json'
os.listdir() returns filenames and not paths. You need to construct the paths from the filenames before calling shutil.copy().
import shutil
import os
source_directory_path = "report"
destination_directory_path = "archived"
for source_filename in os.listdir(source_directory_path):
if source_filename.endswith(".json"):
# construct path from filename
source_file_path = os.path.join(source_directory_path, source_filename)
shutil.copy(source_file_path, destination_directory_path)

Python. IOError: [Errno 13] Permission denied: when i'm copying file

I have two folders: In, Out - it is not system folder on disk D: - Windows 7. Out contain "myfile.txt" I run the following command in python:
>>> shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'D:\\In'
What's the problem?
Read the docs:
shutil.copyfile(src, dst)
Copy the contents (no metadata) of the file named src to a file
named dst. dst must be the complete target file name; look at copy()
for a copy that accepts a target directory path.
use
shutil.copy instead of shutil.copyfile
example:
shutil.copy(PathOf_SourceFileName.extension,TargetFolderPath)
Use shutil.copy2 instead of shutil.copyfile
import shutil
shutil.copy2('/src/dir/file.ext','/dst/dir/newname.ext') # file copy to another file
shutil.copy2('/src/file.ext', '/dst/dir') # file copy to diff directory
I solved this problem, you should be the complete target file name for destination
destination = pathdirectory + filename.*
I use this code fir copy wav file with shutil :
# open file with QFileDialog
browse_file = QFileDialog.getOpenFileName(None, 'Open file', 'c:', "wav files (*.wav)")
# get file name
base = os.path.basename(browse_file[0])
os.path.splitext(base)
print(os.path.splitext(base)[1])
# make destination path with file name
destination= "test/" + os.path.splitext(base)[0] + os.path.splitext(base)[1]
shutil.copyfile(browse_file[0], destination)
First of all, make sure that your files aren't locked by Windows, some applications, like MS Office, locks the oppened files.
I got erro 13 when i was is trying to rename a long file list in a directory, but Python was trying to rename some folders that was at the same path of my files. So, if you are not using shutil library, check if it is a directory or file!
import os
path="abc.txt"
if os.path.isfile(path):
#do yor copy here
print("\nIt is a normal file")
Or
if os.path.isdir(path):
print("It is a directory!")
else:
#do yor copy here
print("It is a file!")
Visual Studio 2019
Solution : Administrator provided full Access to this folder "C:\ProgramData\Docker"
it is working.
ERROR: File IO error seen copying files to volume: edgehubdev. Errno: 13, Error Permission denied : [Errno 13] Permission denied: 'C:\ProgramData\Docker\volumes\edgehubdev\_data\edge-chain-ca.cert.pem'
[ERROR]: Failed to run 'iotedgehubdev start -d "C:\Users\radhe.sah\source\repos\testing\AzureIotEdgeApp1\config\deployment.windows-amd64.json" -v' with error: WARNING! Using --password via the CLI is insecure. Use --password-stdin.
ERROR: File IO error seen copying files to volume: edgehubdev. Errno: 13, Error Permission denied : [Errno 13] Permission denied: 'C:\ProgramData\Docker\volumes\edgehubdev\_data\edge-chain-ca.cert.pem'
use
> from shutil import copyfile
>
> copyfile(src, dst)
for src and dst use:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
This works for me:
import os
import shutil
import random
dir = r'E:/up/2000_img'
output_dir = r'E:/train_test_split/out_dir'
files = [file for file in os.listdir(dir) if os.path.isfile(os.path.join(dir, file))]
if len(files) < 200:
# for file in files:
# shutil.copyfile(os.path.join(dir, file), dst)
pass
else:
# Amount of random files you'd like to select
random_amount = 10
for x in range(random_amount):
if len(files) == 0:
break
else:
file = random.choice(files)
shutil.copyfile(os.path.join(dir, file), os.path.join(output_dir, file))
Make sure you aren't in (locked) any of the the files you're trying to use shutil.copy in.
This should assist in solving your problem
I avoid this error by doing this:
Import lib 'pdb' and insert 'pdb.set_trace()' before 'shutil.copyfile', it would just like this:
import pdb
...
print(dst)
pdb.set_trace()
shutil.copyfile(src,dst)
run the python file in a terminal, it will execute to the line 'pdb.set_trace()', and now the 'dst' file will print out.
copy the 'src' file by myself, and substitute and remove the 'dst' file which has been created by the above code.
Then input 'c' and click the 'Enter' key in the terminal to execute the following code.
well the questionis old, for new viewer of Python 3.6
use
shutil.copyfile( "D:\Out\myfile.txt", "D:\In" )
instead of
shutil.copyfile( r"d:\Out\myfile.txt", r"D:\In" )
r argument is passed for reading file not for copying

Categories

Resources