create file of particular size in python











up vote
27
down vote

favorite
9












I want to create a file of particular size (say, 1GiB).
The content is not important since I will fill stuff into it.



What I am doing is:



f = open("E:\sample", "wb")
size = 1073741824 # bytes in 1 GiB
f.write("" * size)


But this takes too long to finish. It spends me roughly 1 minute.
What can be done to improve this?










share|improve this question




















  • 4




    1073741824 bytes != 1GB. Use an SSD instead of a mechanical HDD? Write to local disk rather than a network share?
    – Johnsyweb
    Jan 11 '12 at 8:17








  • 3




    @Johnsyweb why? isn't that 1024 * 1024 * 1024 ?
    – onemach
    Jan 11 '12 at 9:05






  • 2




    @onemach 1 GB = 10^9 B. 1 GiB = 2^30 B.
    – glglgl
    Jan 11 '12 at 9:38






  • 2




    xkcd - Kilobyte
    – Imran
    Jan 11 '12 at 16:47






  • 2




    @glglgl: Given how few people use gibibyte as a term, possibly due to how ridiculous it sounds (including many file system displays that summarize using base-2 GB instead of base-10 GB), getting pedantic about GiB is just that: Being pedantic. Hard drive manufacturers use base-10 to advertise larger sizes; almost no one else does.
    – ShadowRanger
    Oct 30 '15 at 17:14

















up vote
27
down vote

favorite
9












I want to create a file of particular size (say, 1GiB).
The content is not important since I will fill stuff into it.



What I am doing is:



f = open("E:\sample", "wb")
size = 1073741824 # bytes in 1 GiB
f.write("" * size)


But this takes too long to finish. It spends me roughly 1 minute.
What can be done to improve this?










share|improve this question




















  • 4




    1073741824 bytes != 1GB. Use an SSD instead of a mechanical HDD? Write to local disk rather than a network share?
    – Johnsyweb
    Jan 11 '12 at 8:17








  • 3




    @Johnsyweb why? isn't that 1024 * 1024 * 1024 ?
    – onemach
    Jan 11 '12 at 9:05






  • 2




    @onemach 1 GB = 10^9 B. 1 GiB = 2^30 B.
    – glglgl
    Jan 11 '12 at 9:38






  • 2




    xkcd - Kilobyte
    – Imran
    Jan 11 '12 at 16:47






  • 2




    @glglgl: Given how few people use gibibyte as a term, possibly due to how ridiculous it sounds (including many file system displays that summarize using base-2 GB instead of base-10 GB), getting pedantic about GiB is just that: Being pedantic. Hard drive manufacturers use base-10 to advertise larger sizes; almost no one else does.
    – ShadowRanger
    Oct 30 '15 at 17:14















up vote
27
down vote

favorite
9









up vote
27
down vote

favorite
9






9





I want to create a file of particular size (say, 1GiB).
The content is not important since I will fill stuff into it.



What I am doing is:



f = open("E:\sample", "wb")
size = 1073741824 # bytes in 1 GiB
f.write("" * size)


But this takes too long to finish. It spends me roughly 1 minute.
What can be done to improve this?










share|improve this question















I want to create a file of particular size (say, 1GiB).
The content is not important since I will fill stuff into it.



What I am doing is:



f = open("E:\sample", "wb")
size = 1073741824 # bytes in 1 GiB
f.write("" * size)


But this takes too long to finish. It spends me roughly 1 minute.
What can be done to improve this?







python file






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 11 '12 at 13:49









glglgl

64.9k788163




64.9k788163










asked Jan 11 '12 at 8:09









onemach

2,26532746




2,26532746








  • 4




    1073741824 bytes != 1GB. Use an SSD instead of a mechanical HDD? Write to local disk rather than a network share?
    – Johnsyweb
    Jan 11 '12 at 8:17








  • 3




    @Johnsyweb why? isn't that 1024 * 1024 * 1024 ?
    – onemach
    Jan 11 '12 at 9:05






  • 2




    @onemach 1 GB = 10^9 B. 1 GiB = 2^30 B.
    – glglgl
    Jan 11 '12 at 9:38






  • 2




    xkcd - Kilobyte
    – Imran
    Jan 11 '12 at 16:47






  • 2




    @glglgl: Given how few people use gibibyte as a term, possibly due to how ridiculous it sounds (including many file system displays that summarize using base-2 GB instead of base-10 GB), getting pedantic about GiB is just that: Being pedantic. Hard drive manufacturers use base-10 to advertise larger sizes; almost no one else does.
    – ShadowRanger
    Oct 30 '15 at 17:14
















  • 4




    1073741824 bytes != 1GB. Use an SSD instead of a mechanical HDD? Write to local disk rather than a network share?
    – Johnsyweb
    Jan 11 '12 at 8:17








  • 3




    @Johnsyweb why? isn't that 1024 * 1024 * 1024 ?
    – onemach
    Jan 11 '12 at 9:05






  • 2




    @onemach 1 GB = 10^9 B. 1 GiB = 2^30 B.
    – glglgl
    Jan 11 '12 at 9:38






  • 2




    xkcd - Kilobyte
    – Imran
    Jan 11 '12 at 16:47






  • 2




    @glglgl: Given how few people use gibibyte as a term, possibly due to how ridiculous it sounds (including many file system displays that summarize using base-2 GB instead of base-10 GB), getting pedantic about GiB is just that: Being pedantic. Hard drive manufacturers use base-10 to advertise larger sizes; almost no one else does.
    – ShadowRanger
    Oct 30 '15 at 17:14










4




4




1073741824 bytes != 1GB. Use an SSD instead of a mechanical HDD? Write to local disk rather than a network share?
– Johnsyweb
Jan 11 '12 at 8:17






1073741824 bytes != 1GB. Use an SSD instead of a mechanical HDD? Write to local disk rather than a network share?
– Johnsyweb
Jan 11 '12 at 8:17






3




3




@Johnsyweb why? isn't that 1024 * 1024 * 1024 ?
– onemach
Jan 11 '12 at 9:05




@Johnsyweb why? isn't that 1024 * 1024 * 1024 ?
– onemach
Jan 11 '12 at 9:05




2




2




@onemach 1 GB = 10^9 B. 1 GiB = 2^30 B.
– glglgl
Jan 11 '12 at 9:38




@onemach 1 GB = 10^9 B. 1 GiB = 2^30 B.
– glglgl
Jan 11 '12 at 9:38




2




2




xkcd - Kilobyte
– Imran
Jan 11 '12 at 16:47




xkcd - Kilobyte
– Imran
Jan 11 '12 at 16:47




2




2




@glglgl: Given how few people use gibibyte as a term, possibly due to how ridiculous it sounds (including many file system displays that summarize using base-2 GB instead of base-10 GB), getting pedantic about GiB is just that: Being pedantic. Hard drive manufacturers use base-10 to advertise larger sizes; almost no one else does.
– ShadowRanger
Oct 30 '15 at 17:14






@glglgl: Given how few people use gibibyte as a term, possibly due to how ridiculous it sounds (including many file system displays that summarize using base-2 GB instead of base-10 GB), getting pedantic about GiB is just that: Being pedantic. Hard drive manufacturers use base-10 to advertise larger sizes; almost no one else does.
– ShadowRanger
Oct 30 '15 at 17:14














3 Answers
3






active

oldest

votes

















up vote
34
down vote



accepted










WARNING This solution gives the result that you might not expect. See UPD ...



1 Create new file.



2 seek to size-1 byte.



3 write 1 byte.



4 profit :)



f = open('newfile',"wb")
f.seek(1073741824-1)
f.write(b"")
f.close()
import os
os.stat("newfile").st_size

1073741824


UPD:
Seek and truncate both create sparse files on my system (Linux + ReiserFS). They have size as needed but don't consume space on storage device in fact. So this can not be proper solution for fast space allocation. I have just created 100Gib file having only 25Gib free and still have 25Gib free in result.



Minor Update:
Added b prefix to f.write("") for Py3 compatibility.






share|improve this answer























  • cool, works fine
    – onemach
    Jan 11 '12 at 13:02










  • If your system supports it, the truncate() solution might - strictly seen - be better, as this solution here consumes 1 allocation block for this file although it would not be necessary.
    – glglgl
    Jan 11 '12 at 13:50






  • 2




    Doesn't that generate a sparse file, though? (This may be what was needed, but it's not quite the same)
    – Arafangion
    Jan 11 '12 at 13:51






  • 6




    In python 3 it's f.write(b"")
    – qed
    May 30 '14 at 14:32










  • Works on MacOS High Sierra from a file size enumeration point of view, even though it doesn't affect the overall system size. Great for writing tests for large files, which is exactly what I'm doing :)
    – brandonscript
    Feb 8 at 16:49




















up vote
23
down vote













The question has been answered before. Not sure whether the solution is cross platform, but it works in Windows (NTFS file system) flawlessly.



with open("file.to.create", "wb") as out:
out.truncate(1024 * 1024 * 1024)


This answer uses seek and write:



with open("file.to.create", "wb") as out:
out.seek((1024 * 1024 * 1024) - 1)
out.write('')





share|improve this answer



















  • 7




    Docs: Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content.
    – Janne Karila
    Jan 11 '12 at 8:38










  • @JanneKarila In this case file.to.create is an empty file, so the specified size will always exceed the file's current size?
    – qed
    May 30 '14 at 14:34










  • @qed Yes it will.
    – Janne Karila
    May 30 '14 at 15:36


















up vote
0
down vote













As Arafangion user mentions in his comment, the answers provided so far generate sparse files. To generate a non-sparse file in python (which actually takes up the desired disk space) I use the following code from this other answer:



import os

file_name = 'test_file'
file_size = 1024 # size in bytes
with open(file_name, "wb") as f:
f.write(os.urandom(file_size))


To check if a file is sparse in Linux is use commands ls -lhs and du -h <filename>, compare first column of ls -lhs with output of du -h and if different the file is sparse:



$ ls -lhs    
185548 -rw-r--r-- 1 root root 181.2M Nov 6 22:23 test

$du -h test
182M test


In the example above test is a sparse file.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8816059%2fcreate-file-of-particular-size-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    34
    down vote



    accepted










    WARNING This solution gives the result that you might not expect. See UPD ...



    1 Create new file.



    2 seek to size-1 byte.



    3 write 1 byte.



    4 profit :)



    f = open('newfile',"wb")
    f.seek(1073741824-1)
    f.write(b"")
    f.close()
    import os
    os.stat("newfile").st_size

    1073741824


    UPD:
    Seek and truncate both create sparse files on my system (Linux + ReiserFS). They have size as needed but don't consume space on storage device in fact. So this can not be proper solution for fast space allocation. I have just created 100Gib file having only 25Gib free and still have 25Gib free in result.



    Minor Update:
    Added b prefix to f.write("") for Py3 compatibility.






    share|improve this answer























    • cool, works fine
      – onemach
      Jan 11 '12 at 13:02










    • If your system supports it, the truncate() solution might - strictly seen - be better, as this solution here consumes 1 allocation block for this file although it would not be necessary.
      – glglgl
      Jan 11 '12 at 13:50






    • 2




      Doesn't that generate a sparse file, though? (This may be what was needed, but it's not quite the same)
      – Arafangion
      Jan 11 '12 at 13:51






    • 6




      In python 3 it's f.write(b"")
      – qed
      May 30 '14 at 14:32










    • Works on MacOS High Sierra from a file size enumeration point of view, even though it doesn't affect the overall system size. Great for writing tests for large files, which is exactly what I'm doing :)
      – brandonscript
      Feb 8 at 16:49

















    up vote
    34
    down vote



    accepted










    WARNING This solution gives the result that you might not expect. See UPD ...



    1 Create new file.



    2 seek to size-1 byte.



    3 write 1 byte.



    4 profit :)



    f = open('newfile',"wb")
    f.seek(1073741824-1)
    f.write(b"")
    f.close()
    import os
    os.stat("newfile").st_size

    1073741824


    UPD:
    Seek and truncate both create sparse files on my system (Linux + ReiserFS). They have size as needed but don't consume space on storage device in fact. So this can not be proper solution for fast space allocation. I have just created 100Gib file having only 25Gib free and still have 25Gib free in result.



    Minor Update:
    Added b prefix to f.write("") for Py3 compatibility.






    share|improve this answer























    • cool, works fine
      – onemach
      Jan 11 '12 at 13:02










    • If your system supports it, the truncate() solution might - strictly seen - be better, as this solution here consumes 1 allocation block for this file although it would not be necessary.
      – glglgl
      Jan 11 '12 at 13:50






    • 2




      Doesn't that generate a sparse file, though? (This may be what was needed, but it's not quite the same)
      – Arafangion
      Jan 11 '12 at 13:51






    • 6




      In python 3 it's f.write(b"")
      – qed
      May 30 '14 at 14:32










    • Works on MacOS High Sierra from a file size enumeration point of view, even though it doesn't affect the overall system size. Great for writing tests for large files, which is exactly what I'm doing :)
      – brandonscript
      Feb 8 at 16:49















    up vote
    34
    down vote



    accepted







    up vote
    34
    down vote



    accepted






    WARNING This solution gives the result that you might not expect. See UPD ...



    1 Create new file.



    2 seek to size-1 byte.



    3 write 1 byte.



    4 profit :)



    f = open('newfile',"wb")
    f.seek(1073741824-1)
    f.write(b"")
    f.close()
    import os
    os.stat("newfile").st_size

    1073741824


    UPD:
    Seek and truncate both create sparse files on my system (Linux + ReiserFS). They have size as needed but don't consume space on storage device in fact. So this can not be proper solution for fast space allocation. I have just created 100Gib file having only 25Gib free and still have 25Gib free in result.



    Minor Update:
    Added b prefix to f.write("") for Py3 compatibility.






    share|improve this answer














    WARNING This solution gives the result that you might not expect. See UPD ...



    1 Create new file.



    2 seek to size-1 byte.



    3 write 1 byte.



    4 profit :)



    f = open('newfile',"wb")
    f.seek(1073741824-1)
    f.write(b"")
    f.close()
    import os
    os.stat("newfile").st_size

    1073741824


    UPD:
    Seek and truncate both create sparse files on my system (Linux + ReiserFS). They have size as needed but don't consume space on storage device in fact. So this can not be proper solution for fast space allocation. I have just created 100Gib file having only 25Gib free and still have 25Gib free in result.



    Minor Update:
    Added b prefix to f.write("") for Py3 compatibility.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 26 at 17:07









    Community

    11




    11










    answered Jan 11 '12 at 8:17









    Shamanu4

    2,17221927




    2,17221927












    • cool, works fine
      – onemach
      Jan 11 '12 at 13:02










    • If your system supports it, the truncate() solution might - strictly seen - be better, as this solution here consumes 1 allocation block for this file although it would not be necessary.
      – glglgl
      Jan 11 '12 at 13:50






    • 2




      Doesn't that generate a sparse file, though? (This may be what was needed, but it's not quite the same)
      – Arafangion
      Jan 11 '12 at 13:51






    • 6




      In python 3 it's f.write(b"")
      – qed
      May 30 '14 at 14:32










    • Works on MacOS High Sierra from a file size enumeration point of view, even though it doesn't affect the overall system size. Great for writing tests for large files, which is exactly what I'm doing :)
      – brandonscript
      Feb 8 at 16:49




















    • cool, works fine
      – onemach
      Jan 11 '12 at 13:02










    • If your system supports it, the truncate() solution might - strictly seen - be better, as this solution here consumes 1 allocation block for this file although it would not be necessary.
      – glglgl
      Jan 11 '12 at 13:50






    • 2




      Doesn't that generate a sparse file, though? (This may be what was needed, but it's not quite the same)
      – Arafangion
      Jan 11 '12 at 13:51






    • 6




      In python 3 it's f.write(b"")
      – qed
      May 30 '14 at 14:32










    • Works on MacOS High Sierra from a file size enumeration point of view, even though it doesn't affect the overall system size. Great for writing tests for large files, which is exactly what I'm doing :)
      – brandonscript
      Feb 8 at 16:49


















    cool, works fine
    – onemach
    Jan 11 '12 at 13:02




    cool, works fine
    – onemach
    Jan 11 '12 at 13:02












    If your system supports it, the truncate() solution might - strictly seen - be better, as this solution here consumes 1 allocation block for this file although it would not be necessary.
    – glglgl
    Jan 11 '12 at 13:50




    If your system supports it, the truncate() solution might - strictly seen - be better, as this solution here consumes 1 allocation block for this file although it would not be necessary.
    – glglgl
    Jan 11 '12 at 13:50




    2




    2




    Doesn't that generate a sparse file, though? (This may be what was needed, but it's not quite the same)
    – Arafangion
    Jan 11 '12 at 13:51




    Doesn't that generate a sparse file, though? (This may be what was needed, but it's not quite the same)
    – Arafangion
    Jan 11 '12 at 13:51




    6




    6




    In python 3 it's f.write(b"")
    – qed
    May 30 '14 at 14:32




    In python 3 it's f.write(b"")
    – qed
    May 30 '14 at 14:32












    Works on MacOS High Sierra from a file size enumeration point of view, even though it doesn't affect the overall system size. Great for writing tests for large files, which is exactly what I'm doing :)
    – brandonscript
    Feb 8 at 16:49






    Works on MacOS High Sierra from a file size enumeration point of view, even though it doesn't affect the overall system size. Great for writing tests for large files, which is exactly what I'm doing :)
    – brandonscript
    Feb 8 at 16:49














    up vote
    23
    down vote













    The question has been answered before. Not sure whether the solution is cross platform, but it works in Windows (NTFS file system) flawlessly.



    with open("file.to.create", "wb") as out:
    out.truncate(1024 * 1024 * 1024)


    This answer uses seek and write:



    with open("file.to.create", "wb") as out:
    out.seek((1024 * 1024 * 1024) - 1)
    out.write('')





    share|improve this answer



















    • 7




      Docs: Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content.
      – Janne Karila
      Jan 11 '12 at 8:38










    • @JanneKarila In this case file.to.create is an empty file, so the specified size will always exceed the file's current size?
      – qed
      May 30 '14 at 14:34










    • @qed Yes it will.
      – Janne Karila
      May 30 '14 at 15:36















    up vote
    23
    down vote













    The question has been answered before. Not sure whether the solution is cross platform, but it works in Windows (NTFS file system) flawlessly.



    with open("file.to.create", "wb") as out:
    out.truncate(1024 * 1024 * 1024)


    This answer uses seek and write:



    with open("file.to.create", "wb") as out:
    out.seek((1024 * 1024 * 1024) - 1)
    out.write('')





    share|improve this answer



















    • 7




      Docs: Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content.
      – Janne Karila
      Jan 11 '12 at 8:38










    • @JanneKarila In this case file.to.create is an empty file, so the specified size will always exceed the file's current size?
      – qed
      May 30 '14 at 14:34










    • @qed Yes it will.
      – Janne Karila
      May 30 '14 at 15:36













    up vote
    23
    down vote










    up vote
    23
    down vote









    The question has been answered before. Not sure whether the solution is cross platform, but it works in Windows (NTFS file system) flawlessly.



    with open("file.to.create", "wb") as out:
    out.truncate(1024 * 1024 * 1024)


    This answer uses seek and write:



    with open("file.to.create", "wb") as out:
    out.seek((1024 * 1024 * 1024) - 1)
    out.write('')





    share|improve this answer














    The question has been answered before. Not sure whether the solution is cross platform, but it works in Windows (NTFS file system) flawlessly.



    with open("file.to.create", "wb") as out:
    out.truncate(1024 * 1024 * 1024)


    This answer uses seek and write:



    with open("file.to.create", "wb") as out:
    out.seek((1024 * 1024 * 1024) - 1)
    out.write('')






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 23 '17 at 12:25









    Community

    11




    11










    answered Jan 11 '12 at 8:18









    Imran

    42.9k2085116




    42.9k2085116








    • 7




      Docs: Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content.
      – Janne Karila
      Jan 11 '12 at 8:38










    • @JanneKarila In this case file.to.create is an empty file, so the specified size will always exceed the file's current size?
      – qed
      May 30 '14 at 14:34










    • @qed Yes it will.
      – Janne Karila
      May 30 '14 at 15:36














    • 7




      Docs: Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content.
      – Janne Karila
      Jan 11 '12 at 8:38










    • @JanneKarila In this case file.to.create is an empty file, so the specified size will always exceed the file's current size?
      – qed
      May 30 '14 at 14:34










    • @qed Yes it will.
      – Janne Karila
      May 30 '14 at 15:36








    7




    7




    Docs: Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content.
    – Janne Karila
    Jan 11 '12 at 8:38




    Docs: Note that if a specified size exceeds the file’s current size, the result is platform-dependent: possibilities include that the file may remain unchanged, increase to the specified size as if zero-filled, or increase to the specified size with undefined new content.
    – Janne Karila
    Jan 11 '12 at 8:38












    @JanneKarila In this case file.to.create is an empty file, so the specified size will always exceed the file's current size?
    – qed
    May 30 '14 at 14:34




    @JanneKarila In this case file.to.create is an empty file, so the specified size will always exceed the file's current size?
    – qed
    May 30 '14 at 14:34












    @qed Yes it will.
    – Janne Karila
    May 30 '14 at 15:36




    @qed Yes it will.
    – Janne Karila
    May 30 '14 at 15:36










    up vote
    0
    down vote













    As Arafangion user mentions in his comment, the answers provided so far generate sparse files. To generate a non-sparse file in python (which actually takes up the desired disk space) I use the following code from this other answer:



    import os

    file_name = 'test_file'
    file_size = 1024 # size in bytes
    with open(file_name, "wb") as f:
    f.write(os.urandom(file_size))


    To check if a file is sparse in Linux is use commands ls -lhs and du -h <filename>, compare first column of ls -lhs with output of du -h and if different the file is sparse:



    $ ls -lhs    
    185548 -rw-r--r-- 1 root root 181.2M Nov 6 22:23 test

    $du -h test
    182M test


    In the example above test is a sparse file.






    share|improve this answer

























      up vote
      0
      down vote













      As Arafangion user mentions in his comment, the answers provided so far generate sparse files. To generate a non-sparse file in python (which actually takes up the desired disk space) I use the following code from this other answer:



      import os

      file_name = 'test_file'
      file_size = 1024 # size in bytes
      with open(file_name, "wb") as f:
      f.write(os.urandom(file_size))


      To check if a file is sparse in Linux is use commands ls -lhs and du -h <filename>, compare first column of ls -lhs with output of du -h and if different the file is sparse:



      $ ls -lhs    
      185548 -rw-r--r-- 1 root root 181.2M Nov 6 22:23 test

      $du -h test
      182M test


      In the example above test is a sparse file.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        As Arafangion user mentions in his comment, the answers provided so far generate sparse files. To generate a non-sparse file in python (which actually takes up the desired disk space) I use the following code from this other answer:



        import os

        file_name = 'test_file'
        file_size = 1024 # size in bytes
        with open(file_name, "wb") as f:
        f.write(os.urandom(file_size))


        To check if a file is sparse in Linux is use commands ls -lhs and du -h <filename>, compare first column of ls -lhs with output of du -h and if different the file is sparse:



        $ ls -lhs    
        185548 -rw-r--r-- 1 root root 181.2M Nov 6 22:23 test

        $du -h test
        182M test


        In the example above test is a sparse file.






        share|improve this answer












        As Arafangion user mentions in his comment, the answers provided so far generate sparse files. To generate a non-sparse file in python (which actually takes up the desired disk space) I use the following code from this other answer:



        import os

        file_name = 'test_file'
        file_size = 1024 # size in bytes
        with open(file_name, "wb") as f:
        f.write(os.urandom(file_size))


        To check if a file is sparse in Linux is use commands ls -lhs and du -h <filename>, compare first column of ls -lhs with output of du -h and if different the file is sparse:



        $ ls -lhs    
        185548 -rw-r--r-- 1 root root 181.2M Nov 6 22:23 test

        $du -h test
        182M test


        In the example above test is a sparse file.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 13:32









        dmbarreiro

        11




        11






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8816059%2fcreate-file-of-particular-size-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Liste der Kulturdenkmale in Wilsdruff

            Android Play Services Check