开发者

Migrating to Linux from Windows

I have a python gui that access files on windows as C:\data and C:\MyDIR all outside my doc's. On, a linux sys i created /data and /MyDIR.

My gui cant access. I foresee always using C:\data and C:\MyDIR on both开发者_运维技巧 systems.

How do I fix code or Linux permissions to have access to both dir and sub directories.


Chmod is your friend.

However I question your design. Why do you want to have priviledges at such high levels of file systems. You know every user has a home dir and ther is always dir for configuration on both Windows and Linux.

What you do is a bad practice.


Place the data in the directory Python considers to be the user's home directory on either platform.

home = os.path.expanduser('~')
datadir = os.path.join(home, '.yourappname')

If you don't like prefixing the name with a dot, you might consider using something like this instead:

home = os.path.expanduser('~')
if os.name == 'posix':
    datadir = os.path.join(home, '.yourappname')
else:
    datadir = os.path.join(home, 'Your App Name')

(Also some applications don't use ~/.yourappname any more on Unix-like operating systems, and there is preference for ~/.local/share/yourappname: os.path.join(home, '.local', 'share', 'yourappname').)

The important thing to remember is: put it somewhere in the directory that '~' expands to, and include the name of your app in it to avoid clashes with the data directories of other apps.


i created \data and \MyDIR

First, no you didn't. Paths use / in linux, not \.

Second, do NOT create directories in the root directory unless you know exactly what you're doing. True, you're not going to hurt anything by doing this, but it's extremely bad practice and should be avoided except for specific cases.

Linux is a multi-user OS. If you have configuration files that the user can write to, they should be in the user's home directory somewhere. If you have config files that are read only, they should be installed somewhere such as /etc/.


The linux filesystem uses / as root. You can't use \data and \MyDir because \ does not mean anything. Moreover, the default owner of / is the user named root. Commonly you work with a different user than root on the machine.

So by default, you don't have the permission to write or to create something in / .

Choose another directory in your home directory. For example :

~/data/ & ~/MyDir/

~/ equals to /home/user428862/ where user428862 is your username on the machine.


First of all, maybe you meant to say /data and /MyDIR.

Second, they're direct children of /, the root filesystem, which is reserved to the superuser (root) and people who know what they're doing. Unfortunately the Windows world doesn't enforce nor encourage good practices, so you were able to create those two directories in your C: root (pretty much the analogue of the / directory). Long story short, it's likely you had to use root (probably masked as sudo) to make those two directories inside /, which means root is their owner, and it (and only it) has the permission to write inside them.

You'd better create some alike dirs inside your home (cd ~ and you're there) using your regular user (because you have a regular user, haven't you?) and then use them.

On the other hand you could use something like fuse and ntfs-3g to access those two dirs in the original ntfs filesystem corresponding to C:


Where did you create those directories in your Linux? Under $HOME? You can determine your path separator using the string 'sep' from the os module, that is, os.sep, then acct according to its return value. Comes to my mind sth like:

import os

dirs = [os.sep + "data", os.sep + "MyDIR"]

But all depends on what you want to do. If you can, please explain further your needs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜