I have a file, and several processes which can operate on it. For simplicity let us assume that each process runs the same script: with open(&quot;file.txt&quot;, &quot;r+&quot;) as file:     data = file.read()     data = data + &quot;Hello\n&quot;     file.seek(0)     file.write(data)  I assume that such a way of file access allows several processes to modify the same file, what results in improper writing (for instance, three processes could get &quot;HeHello\nHello\nllo&quot; as a result). Is there any way to forbid access to the file for some time and still be able to read and write from there from the current process?
116620
What is the most elegant way to solve this:   open a file for reading, but only if it is not already opened for writing open a file for writing, but only if it is not already opened for reading or writing   The built-in functions work like this  &gt;&gt;&gt; path = r"c:\scr.txt" &gt;&gt;&gt; file1 = open(path, "w") &gt;&gt;&gt; print file1 &lt;open file 'c:\scr.txt', mode 'w' at 0x019F88D8&gt; &gt;&gt;&gt; file2 = open(path, "w") &gt;&gt;&gt; print file2 &lt;open file 'c:\scr.txt', mode 'w' at 0x02332188&gt; &gt;&gt;&gt; file1.write("111") &gt;&gt;&gt; file2.write("222") &gt;&gt;&gt; file1.close()   scr.txt now contains '111'.  &gt;&gt;&gt; file2.close()   scr.txt was overwritten and now contains '222' (on Windows, Python 2.4).  The solution should work inside the same process (like in the example above) as well as when another process has opened the file. It is preferred, if a crashing program will not keep the lock open.
8399
i wanted to know the Read / Write Access on to a file , so i did this way   ls -l Denem.sh   This command displayed   -rwxr-xr-x  1 pavan employee 672 DEC 20  2000 pavan.sh   Could anybody please tell me what -rwxr-xr-x  indicates ?? From Wikipedia.com i found that  -rwxr-xr-x  indicates     -rwxr-xr-x   for a regular file whose user class has full permissions and whose group and others classes have only the read and execute permissions.   Now my  question is Should we take the middle thing (-xr-) into considiration ??
70393
Is there any way to check whether a file is locked without using a try/catch block?    Right now, the only way I know of is to just open the file and catch any System.IO.IOException.
78088
Is it possible on samba to have only write access, but no read access and no browsing rights?  I'm trying to solve the this access problem:  A windows XP or windows7 machine running within a VM is connected to a samba share.  This machine has to write a text file to a shared directory.  (It is acceptable for the machine to write the file locally and later copy this file to the network.)  The machine may not have rights to read any other files in the shared directory.  It has to give up access to the file it is writing/copying once the file saved fully.  How do I achive this?
3576
I cant find any documentation or articles online online for this...  how does Windows handle page files on multiple hard drives?  Does windows treat them as a concatenation, writing to one till its full then moving to the next?  Or does it treat them like a stripe, writing to each one incrementally?  Does it write to the first one free?  Or perhaps some other method?
98935
I have a sample script (credit goes to ):  #!/bin/bash cat &lt;(cat /etc/lsb-release)   I save this script as somename.sh in my home directory.  Now, I try to open this file in three different ways:  sh somename.sh  bash somename.sh  ./somename.sh   So, I have two questions:   Why are the outputs for the above commands different though they are running the same script?   sh produces a Syntax error bash outputs the desired result ./ gives a Permission Denied error  Why is it necessary to give the script executable permission only when running the script using ./ and not necessary in the other cases?   Thanks in advance!  EDIT: The first part might be similar to the  linked one but, I did have a second question too.
114186
I have two users, user1 and user2, that are both members of groupA. user2 has a folder in their home directory called folderA. If they wish to allow read-write-execute permissions for all members of groupA, how would they do this?  What if folderA contains many files and additional folders that also need to have read-write-execute permission?  Information regarding groups is a little 'spotty' across the web, so I am putting my question here in the hope someone posts a clear answer that might help others out too.  Thanks!
53573
Basically, I would like to check if I have rights to open the file before I actually try to open it; I do not want to use a try/catch for this check unless I have to. Is there a file access property I can check before hand?
25958
I have a log file being written by another process which I want to watch for changes. Each time a change occurs I'd like to read the new data in to do some processing on it.  What's the best way to do this? I was hoping there'd be some sort of hook from the PyWin32 library. I've found the win32file.FindNextChangeNotification function but have no idea how to ask it to watch a specific file.  If anyone's done anything like this I'd be really grateful to hear how...  [Edit] I should have mentioned that I was after a solution that doesn't require polling.  [Edit] Curses! It seems this doesn't work over a mapped network drive. I'm guessing windows doesn't 'hear' any updates to the file the way it does on a local disk.
49965
While logged in, I can do the following:  mkdir foo touch foo/bar chmod 400 foo/bar  chmod 500 foo   Then I can open vim (not as root), edit bar, force a write with w!, and the file is modified.  How can I make the operating system disallow any file modification?  UPDATE Mar 02 2017   chmod 500 foo is a red herring: the write permission on a directory has nothing to do with the ability to modify a file's contents--only the ability to create and delete files. chmod 400 foo/bar does in fact prevent the file's contents from being changed. But, it does not prevent a file's permissions from being changed--a file's owner can always change his file's permissions (assuming they can access the file i.e. execute permission on all ancestor directories). In fact, strace(1) reveals that this is what vim (7.4.576 Debian Jessie) is doing--vim calls chmod(2) to temporarily add the write permission for the file's owner, modifies the file, and then calls chmod(2) again to remove the write permission. That is why using chattr +i works--only root can call chattr -i. Theoretically, vim (or any program) could do the same thing with chattr as it does with chmod on an immutable file if run as root.
96168
I'm trying to create a batch file (to run in the background) that will copy a single file without overwriting any file in the destination with the same name, and then move the file, again not overwriting if there is a file in the destination with the same name?  I'm using this at the moment, but it is overwriting.  copy %1 dest move %1 dest
94205
Having worked with Linux for years, and finding myself with some free time, I decided to revisit some basics. So I re-read the stuff about permissions (without checking source code), and its special cases for folders, and came up with a new (to me at least...) way of thinking about folder permissions (for a specific user/group/others): I imagine a folder as a table with two columns, like so:  filename | inode     foo      | 111   bar      | 222    The read permission means you can read (and list)  the left column of the table, the write permission corresponds to adding and removing entries to the table, and the execute permission corresponds to being able to translate from file name to inode; i.e. you can access the contents of the folder.  I did some experiments, and the results are all consistent with this "worldview" of mine, but one conclusion seems inescapable: that a folder with permissions d-w-------, is totally useless. Elaborating: you can't list its contents, you can't read any files you know exist inside (because you can't translate names into inodes), you can't remove or rename or add files, because again that would imply translation, and you can't even add hardlinks (because, I surmise, that would mean adding a name as well as an inode number, which means you would know both, which in turn, again surmising, violates the purpose of unsetting execution permission). And of course, if there are files inside one such folder, then you can't delete that folder either, because you can't delete its contents.  So... I would like to ask two questions:    Is this analogy of mine correct, or is it a big blunder?  Irrespective of the previous answer, is there any situation where having a folder with permissions as described is appropriate?
66811
I've just heard of this bug "Dirty COW" that allows any user with read access to files to also write to them and gain administrative access. How do I protect against this bug?
54930
I'm looking to write a script that takes a .txt filename as an argument, reads the file line by line, and passes each line to a command. For example, it runs command --option "LINE 1", then command --option "LINE 2", etc. The output of the command is written to another file. How do I go about doing that? I don't know where to start.
95309
For the purpose of this question, let us assume that there is a file (/path/to/file) in the filesystem with a certain content like this: $&gt; cat /path/to/file this is the content of the file /path/to/file   I am happy with almost all processes running (and having reading access to this file), reading this content. There should however (and this is the core of the question) be a specific process, which when reading from /path/to/file should be provided with different file content. $&gt; cat /path/to/file this is the DIFFERNT content of the file /path/to/file   How can I somehow spoof the file content for a specific process? Some guesses of mine, which road to take would be solitions related to :  symlinking trick (linux) namespaces (filesystem namespace) trick chroot trick $LD_PRELOAD hooking trick overlayfs  My platform is GNU/linux, but if there is a POSIX way to achieve it that would be even better :) Update What would be a good solution/answer? A criterium for a good solution/answer would be that it is possible to achieve &quot;different file content for specific process&quot;, without needing interaction of the root user, eventhough the ideally the spoofed file should not be user-writeable in the first place. Another good criterium would be that the modification of the file process shown to the process is specific, and ideally without sort of race-condition. Background Mozilla firefox uses 2 zip archives /usr/lib/firefox/omni.ja and /usr/lib/firefox/browser/omni.ja, which contain a fair deal of firefox' code (mostly  the stuff writen in Javascript), by spoofing the file I would be able to modify my version of firefox (include some features I cannot anymore implement as a extension, provided dropped XPCOM support and disliked addon-siging coercion)
57019
Normally we use a different file to redirect the output.  For example :  cat &lt; first &gt; temp   In this command the contents of first are redirected to temp instead of the standard output.  Then why does it truncates the file if I use the same file name?  Why can't it overwrite the same file?  cat &lt; first &gt; first
96825
Say I have the following script:  #!/bin/bash for i in $(seq 1000) do     cp /etc/passwd tmp     cat tmp | head -1 | head -1 | head -1 &gt; tmp  #this is the key line     cat tmp done   On the key line, I read and write the same file tmp which sometimes fails.  (I read it is because of race conditions because the processes in the pipeline are executed in parallel, which I do not understand why - each head needs to take the data from the previous one, doesn't it? This is NOT my main question, but you can answer it as well.)  When I run the script, it outputs about 200 lines. Is there any way I can force this script to output always 0 lines (so the I/O redirection to tmp is always prepared first and so the data is always destroyed)? To be clear, I mean changing the system settings, not this script.  Thanks for your ideas.