473,698 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to determine if files are on same or different file systems

Thanks to everyone on this list. I now have a functioning piece of
python code!! :-))

Now I'm trying to clean it up.

I have the same (or similar) lines repeated several times:

shutil.copy2(ne wArrivalsDir+'/'+movie,archive sDir)
thumb = string.replace( movie,'.avi','. jpg')
shutil.copy2(ne wArrivalsDir+'/tn/'+thumb,archive sDir+'/tn/')

or

os.rename(other FavDir+'/'+movie,dir+'/'+movie)
thumb = string.replace( movie,'.avi','. jpg')
os.rename(other FavDir+'/tn/'+thumb,dir+'/tn'+thumb)

what varies is the name of the function (shutil.copy2 or os.rename
depending on if I am renaming or copying) and the names of the source
and dest directories. This particular snippet is repeated about a
half-dozen times.

It would be nice if I could write a function that would determine if the
source and destination are on the same file system or not, and thus use
rename or copy appropriately, or if there is already such a built-in
function.

TIA,

-Kamus

--
What am I on?
I'm on my bike, o__
6 hours a day, busting my ass. ,>/'_
What are you on? --Lance Armstrong (_)\(_)

Jul 18 '05 #1
7 1939
It would be nice if I could write a function that would determine if the
source and destination are on the same file system or not, and thus use
rename or copy appropriately, or if there is already such a built-in
function.


shutil.move ? (Introduced in Python 2.3)
Jul 18 '05 #2

Kamus> It would be nice if I could write a function that would determine
Kamus> if the source and destination are on the same file system or not,
Kamus> and thus use rename or copy appropriately, or if there is already
Kamus> such a built-in function.

You might want to check out os.path.ismount (). Using it, you can walk up
the two paths toward the root until you hit elements of bot paths where
os.path.ismount () returns True. If the two mount points are the same, the
two original paths are on the same file system. Here's some sample code.
It doesn't help much on my Mac because it essentially has just a single disk
partition. On my web server (Linux w/ multiple partitions), it seems to
work okay:

% df
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda1 248847 93239 142758 40% /
/dev/hda6 14333167 9393563 4194190 70% /backup
none 385572 0 385572 0% /dev/shm
/dev/hda5 9867356 7702853 1652433 83% /home
/dev/hda7 1981000 1568913 309675 84% /usr
/dev/hda8 248847 120886 115111 52% /var
% python samefs.py
('/', '/usr') 0
('/bin', '/etc') 1
('/etc/passwd', '/var/log') 0
('/Applications', '/Volumes Kisses In The Rain') 0

Skip

import os

def samefs(path1, path2):
if not (os.path.exists (path1) and os.path.exists( path2)):
return False

while path1 != os.path.dirname (path1):
if os.path.ismount (path1):
break
path1 = os.path.dirname (path1)

while path2 != os.path.dirname (path2):
if os.path.ismount (path2):
break
path2 = os.path.dirname (path2)

return path1 == path2

print ("/", "/usr"), samefs("/", "/usr")
print ("/bin", "/etc"), samefs("/bin", "/etc")
print ("/etc/passwd", "/var/log"), samefs("/etc/passwd", "/var/log")
print ("/Applications", "/Volumes Kisses In The Rain"), \
samefs("/Applications", "/Volumes Kisses In The Rain")
Jul 18 '05 #3
Kamus of Kadizhar wrote:
Thanks to everyone on this list. I now have a functioning piece of
python code!! :-))

Now I'm trying to clean it up.

I have the same (or similar) lines repeated several times:

shutil.copy2(ne wArrivalsDir+'/'+movie,archive sDir)
thumb = string.replace( movie,'.avi','. jpg')
shutil.copy2(ne wArrivalsDir+'/tn/'+thumb,archive sDir+'/tn/')

or

os.rename(other FavDir+'/'+movie,dir+'/'+movie)
thumb = string.replace( movie,'.avi','. jpg')
os.rename(other FavDir+'/tn/'+thumb,dir+'/tn'+thumb)

what varies is the name of the function (shutil.copy2 or os.rename
depending on if I am renaming or copying) and the names of the source
and dest directories. This particular snippet is repeated about a
half-dozen times.
Probably time to turn it into a function

def moveMovieAndThu mb(fromDir, toDir, movieName):
# your code

or similar.
It would be nice if I could write a function that would determine if the
source and destination are on the same file system or not, and thus use
rename or copy appropriately, or if there is already such a built-in
function.


No need to determine it beforehand, just try (untested):

def movefile(src, dst):
try:
os.rename(src, dst)
except OSError:
shutil.copy2(sr c, dst)
os.remove(src)
Of course, if you are using Python 2.3 you should use shutil.move() as
pointed out by Serge Orlov; the above was mostly posted to illustrate the
popular concept "It's easier to ask forgiveness than permission", i. e.
with Python's powerful exception handling mechanism you need not fear the
failure of a particular code snippet, as long as you provide the
appropriate error handling.

Random remarks:
- You might take a look at os.path.join()
- Ok, this is paranoia, but I would ensure that ".avi" is only at the end of
the string
- Use methods of the str object rather than functions in the string module,
e. g. "abc".replace(" a", "d") rather than string.replace( "abc", "a", "d")

Peter
Jul 18 '05 #4
Peter Otten wrote:
No need to determine it beforehand, just try (untested):

def movefile(src, dst):
try:
os.rename(src, dst)
except OSError:
shutil.copy2(sr c, dst)
os.remove(src)
Of course, if you are using Python 2.3 you should use shutil.move() as
pointed out by Serge Orlov; the above was mostly posted to illustrate the
popular concept "It's easier to ask forgiveness than permission", i. e.
with Python's powerful exception handling mechanism you need not fear the
failure of a particular code snippet, as long as you provide the
appropriate error handling.
Peter:

I am copying 600mb - 1.2gb files over slow wireless to an NFS mount -
not exactly the model of reliability. It takes 40 minutes to an hour to
copy one of these. I am paranoid of failures - I don't want to lose movies.

So, I don't want to rely on innate error handling, because the failure
could come from wireless failure, and the copy could fail as well.
That's why I'm looking for a way to tell if a particular partition is
network mounted.

The basic idea is that movies get moved on the same partition, but
copied (and not deleted) when moving between network mounted partitions
and local partitions. Deletes will be subject to manual review until I
get a comfort factor.

Random remarks:
- You might take a look at os.path.join()
- Ok, this is paranoia, but I would ensure that ".avi" is only at the end of
the string
".avi$" ? I still haven't figured out Python's string handling really
well. Or use the length of the string and count backwards? I admit, I
got lazy on this one.
- Use methods of the str object rather than functions in the string module,
e. g. "abc".replace(" a", "d") rather than string.replace( "abc", "a", "d")


OK, I'll try that.

-Kamus

--
What am I on?
I'm on my bike, o__
6 hours a day, busting my ass. ,>/'_
What are you on? --Lance Armstrong (_)\(_)

Jul 18 '05 #5
Kamus of Kadizhar wrote:
Peter Otten wrote:
No need to determine it beforehand, just try (untested):

def movefile(src, dst):
try:
os.rename(src, dst)
except OSError:
shutil.copy2(sr c, dst)
os.remove(src)
Of course, if you are using Python 2.3 you should use shutil.move() as
pointed out by Serge Orlov; the above was mostly posted to illustrate the
popular concept "It's easier to ask forgiveness than permission", i. e.
with Python's powerful exception handling mechanism you need not fear the
failure of a particular code snippet, as long as you provide the
appropriate error handling.


Peter:

I am copying 600mb - 1.2gb files over slow wireless to an NFS mount -
not exactly the model of reliability. It takes 40 minutes to an hour to
copy one of these. I am paranoid of failures - I don't want to lose
movies.

So, I don't want to rely on innate error handling, because the failure
could come from wireless failure, and the copy could fail as well.
That's why I'm looking for a way to tell if a particular partition is
network mounted.

The basic idea is that movies get moved on the same partition, but
copied (and not deleted) when moving between network mounted partitions
and local partitions. Deletes will be subject to manual review until I
get a comfort factor.


I still think that EAFP

try:
os.rename(src, dst)
except OSError:
# any error here will still result in a traceback
# if not handled in client code
shutil.copy2(sr c, dst)
# os.remove() ommitted intentionally

is superior style to LBYL (look before you leap)

if isSamePartition (src, dst):
os.rename(src, dst)
else:
shutil.copy2(sr c, dst)

and fail to see the increased danger for your data. Plus, it saves you from
writing the nonexistent isSamePartition () :-)
Maybe you could replace the copy2() call with the invocation of some utility
that reliably copies over an unreliable connection (maybe can someone else
come up with a name), or you take the extra amount of work/time and compare
the two files after you have copied them.

Random remarks:
- You might take a look at os.path.join()
- Ok, this is paranoia, but I would ensure that ".avi" is only at the end
of the string


".avi$" ? I still haven't figured out Python's string handling really
well. Or use the length of the string and count backwards? I admit, I
got lazy on this one.


Use regular expressions only as a last resort.
import os
path = "/dir/name.ext"
os.path.splitex t(path) ('/dir/name', '.ext')

Putting it into a function:
def changeext(path, newext=""): .... return os.path.splitex t(path)[0] + newext
.... changeext("/sowhat/movie.mpg", ".jpg") '/sowhat/movie.jpg' changeext("/sowhat/movie.mpg") '/sowhat/movie' changeext("/sowhat/movie", ".jpg") '/sowhat/movie.jpg'

I you want to do it only with string methods:
path = "/one/two.three"
path[:path.rindex(". ")] + ".four" '/one/two.four'

However, you would have to ensure that the dot position is after the last
slash.

For a quick overview what is available for strings:
dir(str)

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__',
'__ge__', '__getattribute __', '__getitem__', '__getnewargs__ ',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__' , '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith',
'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower',
'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
'replace', 'rfind', 'rindex', 'rjust', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Peter
Jul 18 '05 #6
You can find the device a file is on using the .st_dev field of its stat
information:
os.stat("/").st_dev see the stat manpage for more details.

You can find the magic number for a particular filesystem using statfs(2),
but as far as I can tell this is not exposed in Python. If it were,
you'd write something like os.statfs("/").f_type

to get the type of the filesystem where / resides. Some f_type values
are mentioned in my statfs(2) manpage, including
NFS_SUPER_MAGIC 0x6969
os.statvfs() seems to be unrelated, though my system lacks a manpage for
it.

Jeff

Jul 18 '05 #7
On Sat, 06 Dec 2003 17:15:42 -0500, rumours say that Kamus of Kadizhar
<ya*@NsOeSiPnAe Mr.com> might have written:
It would be nice if I could write a function that would determine if the
source and destination are on the same file system or not, and thus use
rename or copy appropriately, or if there is already such a built-in
function.


The os.stat call result has a st_dev (the device) field that could be
very useful to you. It works for Windows too, where you can use extra
calls from win32api or win32file, can't remember which to know the file
system type too.
--
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
19280
by: Hal Vaughan | last post by:
I am writing out archive files using ZipOutputStream with the following code: aEntry is a global Array of ZipEntries llData is a LinkedList of the data corresponding to the the ZipEntry of the same index in aEntry public void save() { byte bBuffer = null; int i = 0;
18
3175
by: JKop | last post by:
Here's what I know so far: You have a C++ project. You have source files in it. When you go to compile it, first thing the preprocessor sticks the header files into each source file. So now you have your ".cpp" files all ready, without any "#include" or "#define" in them. Let's assume that there's 2 source files in this project, "a.cpp" and
3
7031
by: Ben | last post by:
Hi all - I am having a bit of trouble and thought maybe someone in this group could shed some light. Here's the skinny... I am creating an automated process to import a bunch of text files into Access. I want to avoid creating a separate "Spec" for each file (there are over 180 files) and instead want to code my own dynamic importing rules. So far it's been going fine, except for one item...
6
14326
by: Kaki | last post by:
Given a file, how do I know if it's ascii or unicode or binary? And how do I know if it's rtf or html or etc? In other words, how do I find the stream type or mime type? (No, file extension cannot be the answer) Thanks *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
3
1812
by: David | last post by:
I want to develop a single web app that will be run in three environments: development, test, and production. The test and prodc. will be on the same machine under different directories. I wish to have same type of file, configuration string, etc. in the root directory of each environment to tell the program where it is running. I will use this to determine what connection string, sql, etc. to use. Currently, I am using 3 different...
8
9802
by: Dinesh Jain | last post by:
Hi all, I encountered a serious problem while working with clipboard class in VB.NET. I want to simulate cut-copy-paste operation with my application and Windows Explorer. User can copy files from Explorer and paste it into my application & vice-a-versa. My question is- How can I determine if user has copied or cut the files from Windows Explorer? I want to differentiate cut & copy.
19
2637
by: Chad | last post by:
Okay, let's say I have an exotic os that limits how much goes to stdout. When I go like.. #include <stdio.h> int main(void) { int i=0; for(i=0; i< 10; i++) { printf("a \n");
3
3843
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into the two problems while trying to read a binary file. 1). All whitespace characters were skipped 2). Certain binary files gave a core dump
28
2492
by: SzH | last post by:
Suppose that there is a program that takes two files as its command line arguments. Is there a (cross platform) way to decide whether the two files are the same? Simple string comparison is not enough as the two files might be specified as "file.txt" and "./file.txt", or one of them may be a symlink to the other.
0
8668
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6515
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.