How to insert strings and slashes in a path? - python

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

Related

Remove special character from multiple file names in folder in python

I am trying to remove special character(-) from multiple files in folder.
Example:
Filenames :
-name1.xml
-name2.xml
-name3.xml
Rename To:
name1.xml
name2.xml
name3.xml
My Code :
import os
for filename in os.listdir(Folder):
os.rename(Folder+'/'+filename, Folder + '/' + Filename.replace("-","" )
but Unfortunately it appears to do nothing.
How do I do this properly?
In the code below, you can simply use the replace function to get your desired output. Let me know if this works and/or helps!
import os
# I named my folder containing .XML files "Test"
Folder = 'Test/'
for filename in os.listdir(Folder):
os.rename(Folder + filename, Folder + filename.replace('-',''))
Outputs:
name1.xml
name2.xml
name3.xml
I am a big fan of using Pathlib for problems like this.
Given (on Unix but should work on all OSs):
% ls -1
-name1.xml
-name2.xml
-name3.xml
name-4.xml
You can do:
from pathlib import Path
p=Path(Folder)
for fn in p.glob("-*.xml"):
fn.replace(fn.with_name(str(fn.name).replace('-','')))
Result:
% ls -1
name-4.xml
name1.xml
name2.xml
name3.xml
The glob -*.xml only will find files that start with - so the file name-4.xml is unchanged.
Pathlib (as written here) will also work the same regardless of the path separator on different OSs.

Problems while getting relative paths of a file?

I am reading in a different machine a file. Therefore, I need to access to the full path of the file. So I tried to use pythons Pathlib module:
a_path = '/dir1/subdir1/sample.txt'
home = str(Path.home())
a_path = str(home) + str(a_path)
Apparently, the above code return me the full path. However, when I read it I get:
FileNotFoundError: [Errno 2] No such file or directory: "/home/user'/dir1/subdir1/sample.txt'"
How can I fix the above error? maybe in the concatenation I am getting problems.
Try this. This uses os.path.join which joins two paths together
import os
import pathlib
a_path = 'dir1/subdir1/sample.txt'
home = str(pathlib.Path.home())
print(os.path.join(home, a_path))
#/home/user/dir1/subdir1/sample.txt
You can use join to paste the string together.
''.join([str(Path.home()), 'path\\t.txt'])
First of all, '/dir1/subdir1/sample.txt' is an absolute path. If you want it to be a relative path (which seems to be the case) you should use 'dir1/subdir1/sample.txt', so without a leading /.
Using the pathlib library this then becomes very easy
>>> from pathlib import Path
>>> a_path = "dir1/subdir1/sample.txt"
>>> a_path = Path.home() / a_path
>>> print(a_path)
/home/pareto/dir1/subdir1/sample.txt
Again, make sure you are not using absolute paths. Otherwise you would get the following
>>> print(Path.home() / "/dir1/subdir1/sample.txt")
/dir1/subdir1/sample.txt

Using input to change directory path

I'm kinda new to python and I feel like the answer to this is so simple but I have no idea what the answer is. I'm trying to move files from one place to another but I don't want to have to change my code every time I wanna move that file so I just want to get user input from the terminal.
import shutil
loop = True
while loop:
a = input()
shutil.move("/home/Path/a", "/home/Path/Pictures")
What do I have to put around the a so that it doesn't read it as part of the string?
This should do what you want. the os.path.join() will combine the string value in a, that you get from input with the first part of the path you have provided. You should use os.path.join() as this will form paths in a way that is system independent.
import shutil
import os
loop = True
while loop:
a = input()
shutil.move(os.path.join("/home/Path/", a), "/home/Path/Pictures")
Output:
>>> a = input()
test.txt
>>> path = os.path.join("/home/Path/", a)
>>> path
'/home/Path/test.txt'
You can also use "/home/Path/{0}".format(a) which will swap the value of a with {0}, or you can do do "/home/Path/{0}" + str(a) which will also do what you want.
Edited to account for Question in comment:
This will work if your directory doesn't have any sub-directories. it may still work if there are directories and files in there but I didn't test that.
import shutil
import os
files = os.listdir("/home/Path/")
for file in files:
shutil.move(os.path.join("/home/Path/", file), "/home/Path/Pictures")
one solution
a = 'test.csv'
path = '/home/Path/{}'.format(a)
>>> path
/home/Path/test.csv

Python, Opening files in loop (dicom)

I am currently reading in 200 dicom images manually using the code:
ds1 = dicom.read_file('1.dcm')
so far, this has worked but I am trying to make my code shorter and easier to use by creating a loop to read in the files using this code:
for filename in os.listdir(dirName):
dicom_file = os.path.join("/",dirName,filename)
exists = os.path.isfile(dicom_file)
print filename
ds = dicom.read_file(dicom_file)
This code is not currently working and I am receiving the error:
"raise InvalidDicomError("File is missing 'DICM' marker. "
dicom.errors.InvalidDicomError: File is missing 'DICM' marker. Use
force=True to force reading
Could anyone advice me on where I am going wrong please?
I think the line:
dicom_file = os.path.join("/",dirName,filename)
might be an issue? It will join all three to form a path rooted at '/'. For example:
os.path.join("/","directory","file")
will give you "/directory/file" (an absolute path), while:
os.path.join("directory","file")
will give you "directory/file" (a relative path)
If you know that all the files you want are "*.dcm"
you can try the glob module:
import glob
files_with_dcm = glob.glob("*.dcm")
This will also work with full paths:
import glob
files_with_dcm = glob.glob("/full/path/to/files/*.dcm")
But also, os.listdir(dirName) will include everything in the directory including other directories, dot files, and whatnot
Your exists = os.path.isfile(dicom_file) line will filter out all the non files if you use an "if exists:" before reading.
I would recommend the glob approach, if you know the pattern, otherwise:
if exists:
try:
ds = dicom.read_file(dicom_file)
except InvalidDicomError as exc:
print "something wrong with", dicom_file
If you do a try/except, the if exists: is a bit redundant, but doesn't hurt...
Try adding:
dicom_file = os.path.join("/",dirName,filename)
if not dicom_file.endswith('.dcm'):
continue

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