Create a file object using the open() function. Along with the file name, specify: 'r' for reading in an existing file (default; ...
howcanpythoncheckifafilenameisinutf8?LastUpdated:TueAug232022ThechallengeyouhaveistoconvertyourfilenamestobeWindows1252compatible.Asyou'vestatedinyourquestion,itwouldbeidealnottorenamefilesthatarealreadycompatible.I'vereworkedyourattemptas:importos
fromglob
importglob
importshutil
importurllib
files=glob(u'*.txt')
formy_fileinfiles:
try:
print"File%s"%my_file
exceptUnicodeEncodeError:
print"File(escaped):%s"%my_file.encode("unicode_escape")
new_name=my_file
try:
my_file.encode("cp1252","strict")
print"Nameunchanged.Copyinganyway"
exceptUnicodeEncodeError:
print"Cannotconverttocp1252"
utf_8_name=my_file.encode("UTF-8")
new_name=urllib.quote(utf_8_name)
print"Newname:(%%encoded):%s"%new_name
shutil.copy2(my_file,os.path.join("fixed",new_name))Astrobjectcanbeencodedinbytes:>>>a='a'
>>>type(a)
>>>a.encode
>>>a.decode
Traceback(mostrecentcalllast):
File"",line1,in
AttributeError:'str'objecthasnoattribute'decode'...whileabytesobjectcanbedecodedinstr:>>>b=b'b'
>>>type(b)
>>>b.decode
>>>b.encode
Traceback(mostrecentcalllast):
File"",line1,in
AttributeError:'bytes'objecthasnoattribute'encode'Suggestion:2ThebaseCodecclassdefinesthesemethodswhichalsodefinethe
functioninterfacesofthestatelessencoderanddecoder:,Thecodecsmoduledefinesasetofbaseclasseswhichdefinethe
interfacesforworkingwithcodecobjects,andcanalsobeusedasthebasis
forcustomcodecimplementations.,TheIncrementalDecoderclassisusedfordecodinganinputinmultiple
steps.Itdefinesthefollowingmethodswhicheveryincrementaldecodermust
defineinordertobecompatiblewiththePythoncodecregistry.,TheStreamReaderclassisasubclassofCodecanddefinesthe
followingmethodswhicheverystreamreadermustdefineinordertobe
compatiblewiththePythoncodecregistry.>>>'Germanß,♬'.encode(encoding='ascii',errors='backslashreplace')
b'German\\xdf,\\u266c'>>>
'Germanß,♬'.encode(encoding='ascii',errors='xmlcharrefreplace')
b'Germanß,♬'Suggestion:3Createafileobjectusingtheopen()function.Alongwiththefilename,specify:
'r'forreadinginanexistingfile(default;canbedropped),
'w'forcreatinganewfileforwriting,
'a'forappendingnewcontenttoanexistingfile.
,'r'forreadinginanexistingfile(default;canbedropped),
,Dosomethingwiththefileobject(reading,writing).
,Closethefileobjectbycallingthe.close()methodonthefileobject.
myfile=open('alice.txt','r')#Reading.
'r'
canbeomitted
#...readfrommyfile...
myfile.close()#Closingfile
foo.pymyfile=open('results.txt','w')#Thefileisnewlycreatedwherefoo.pyis
#...writetomyfile...
myfile.close()#Closingfile.VERYIMPORTANT!
myfile=open('results.txt','a')#'a':appendinginsteadofoverwriting.
#...addtexttothefile...
myfile.close()#Closingfile.DON'TFORGET!
foo.pymyfile=open('alice.txt',encoding='utf-8')#ReadingaUTF-8file;
'r'
isomitted
myfile=open('results.txt','w',encoding='utf-8')#FilewillbewritteninUTF-8
foo.py>>>f=open('mary-short.txt')
>>>marytxt=f.read()#Using.read()
>>>f.close()
>>>marytxt
'Maryhadalittlelamb,\nHisfleecewaswhiteassnow,\nAndeverywherethatMary
went,\nThelambwassuretogo.\n'
>>>type(marytxt)#marytxtisstringtype
>>>len(marytxt)#marytxthas110characters
110
>>>print(marytxt[0])
M>>>f=open('mary-short.txt')
>>>marylines=f.readlines()#Using.readlines()
>>>f.close()
>>>marylines
['Maryhadalittlelamb,\n','Hisfleecewaswhiteassnow,\n','Andeverywhere
thatMarywent,\n','Thelambwassuretogo.\n']
>>>type(marylines)#marylinesislisttype
>>>len(marylines)#marylineshas4lines
4
>>>print(marylines[0])
Maryhadalittlelamb,f=open('bible-kjv.txt')#Thisisabigfile
forlineinf:#Using'for...in'
onfileobject
if'smite'inline:
print(line,)#','
keepsprintfromaddingaline
break
f.close()
foo.pySuggestion:4fileObj.read()->str:Readtheentirefileintoastring.,fileObj.readlines()->[str]:Readalllinesintoalistofstrings.,fileObj.readline()->str:(mostcommonly-used)Readnextline(uptoandincludenewline)andreturnastring(includingnewline).Itreturnsanemptystringaftertheend-of-file(EOF).,fileObj.seek(offset):setsthecurrentstreampositiontooffsetbytesfromthebeginningofthefile.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#!/usr/bin/envpython3
#-*-coding:UTF-8-*-
"""
file_copy:Copyfileline-by-linefromsourcetodestination
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage:file_copy
"""
importsys
importos
defmain():
#Checkandretrievecommand-linearguments
iflen(sys.argv)!=3:
print(__doc__)
sys.exit(1)#Returnanon-zerovaluetoindicateabnormaltermination
fileIn=sys.argv[1]
fileOut=sys.argv[2]
#Verifysourcefile
ifnotos.path.isfile(fileIn):
print("error:{}doesnotexist".format(fileIn))
sys.exit(1)
#Verifydestinationfile
ifos.path.isfile(fileOut):
print("{}exists.Override(y/n)?".format(fileOut))
reply=input().strip().lower()
ifreply[0]!='y':
sys.exit(1)
#Processthefileline-by-line
withopen(fileIn,'r')asfpIn,open(fileOut,'w')asfpOut:
lineNumber=0
forlineinfpIn:
lineNumber+=1
line=line.rstrip()#Striptrailingspacesandnewline
fpOut.write("{}:{}\n".format(lineNumber,line))
#Need\n,whichwillbetranslatedtoplatform-dependentnewline
print("Numberoflines:{}\n".format(lineNumber))
if__name__=='__main__':
main()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#!/usr/bin/envpython3
#-*-coding:UTF-8-*-
"""
file_list_oswalk.py-Listfilesrecursivelyfromagivendirectory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage:files_list_oswalk.py[|.]
"""
importsys
importos
defmain():
#Processcommand-linearguments
iflen(sys.argv)>2:#Command-lineargumentsarekeptinalist'sys.argv'
print(__doc__)
sys.exit(1)#Returnanon-zerovaluetoindicateabnormaltermination
eliflen(sys.argv)==2:
dir=sys.argv[1]#directorygivenincommand-lineargument
else:
dir='.'#defaultcurrentdirectory
#Verifydir
ifnotos.path.isdir(dir):
print('error:{}doesnotexists'.format(dir))
sys.exit(1)
#Recursivelywalkthrufromdirusingos.walk()
forcurr_dir,subdirs,filesinos.walk(dir):
#os.walk()recursivelywalkthruthegiven"dir"anditssub-directories
#Foreachiteration:
#-curr_dir:currentdirectorybeingwalkthru,recursivelyfrom"dir"
#-subdirs:listofsub-directoriesin"curr_dir"
#-files:listoffiles/symlinksin"curr_dir"
print('D:',os.path.abspath(curr_dir))#printcurrentlywalkdir
forsubdirinsorted(subdirs):#printallsubdirsunder"curr_dir"
print('SD:',os.path.abspath(subdir))
forfileinsorted(files):#printallfilesunder"curr_dir"
print(os.path.join(os.path.abspath(curr_dir),file))#fullfilename
if__name__=='__main__':
main()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43#!/usr/bin/envpython3
#-*-coding:UTF-8-*-
"""
file_list_glob.py-Listfilesrecursivelyfromagivendirectory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage:files_list[|.]
"""
importsys
importos
importglob#Python3.5
defmain():
#Processcommand-linearguments
iflen(sys.argv)>2:#Command-lineargumentsarekeptinalist'sys.argv'
print(__doc__)
sys.exit(1)#Returnanon-zerovaluetoindicateabnormaltermination
eliflen(sys.argv)==2:
dir=sys.argv[1]#directorygivenincommand-lineargument
else:
dir='.'#defaultcurrentdirectory
#Checkdir
ifnotos.path.isdir(dir):
print('error:{}doesnotexists'.format(dir))
sys.exit(1)
#List*.txtonly
forfileinglob.glob(dir+'/**/*.txt',recursive=True):
#**matchanyfilesandzeroormoredirectoriesandsubdirectories
print(file)
print('----------------------------')
#Listallfilesandsubdirs
forfileinglob.glob(dir+'/**',recursive=True):
#**matchanyfilesandzeroormoredirectoriesandsubdirectories
ifos.path.isdir(file):
print('D:',file)
else:
print(file)
if__name__=='__main__':
main()SimilarArticles1.)Pythonchallengingstringencoding2.)UTF-8BOMsignatureinPHPfiles3.)PHP:ConvertunicodecodepointtoUTF-84.)WhatfactorsmakePHPUnicode-incompatible?5.)ConvertUTF-16LEtoUTF-8inphp6.)Phputf-8decodefromxmlreturnsquestionmarks7.)XMLfileoutputonlyshowsByteOrderMark8.)VBAOutputtofileusingUTF-169.)MapsupplementaryUnicodecharacterstoBMP(ifpossible)10.)HowtoaddaUTF-8BOMinJava?TrendingTechnologyandroid×13870angular×16962api×4899css×14556html×21320java×28499javascript×57492json×17645php×21600python×502736reactjs×16351sql×19874typescript×7220xml×2600Mostpopularinpython1.)settingpathinterminal2.)pythonmultiprocessing:endinganinfinitecounter3.)whatdoessetup.pybuilddo?4.)loginintowebsiteanddownloadfilewithpythonrequests5.)part-of-speechtagwithoutcontextusingnltk6.)properwaytoimplementpython-social-authwithdjangoandsteambackend7.)bestapproachtodetectsubnetoverlapinapostgresqldb8.)howtogethttpversioninflask9.)howcaniremovethegreenarrowfromacellonexcelfileusingpython-xlsxwriter?10.)gettingstartedwithfirefoxadd-onsdk