tf.compat.v1.gfile.Copy
Stay organized with collections
Save and categorize content based on your preferences.
Copies data from src
to dst
.
tf.compat.v1.gfile.Copy(
oldpath, newpath, overwrite=False
)
with open("/tmp/x", "w") as f:
f.write("asdf")
4
tf.io.gfile.exists("/tmp/x")
True
tf.io.gfile.copy("/tmp/x", "/tmp/y")
tf.io.gfile.exists("/tmp/y")
True
tf.io.gfile.remove("/tmp/y")
You can also specify the URI scheme for selecting a different filesystem:
with open("/tmp/x", "w") as f:
f.write("asdf")
4
tf.io.gfile.copy("/tmp/x", "file:///tmp/y")
tf.io.gfile.exists("/tmp/y")
True
tf.io.gfile.remove("/tmp/y")
Note that you need to always specify a file name, even if moving into a new
directory. This is because some cloud filesystems don't have the concept of a
directory.
with open("/tmp/x", "w") as f:
f.write("asdf")
4
tf.io.gfile.mkdir("/tmp/new_dir")
tf.io.gfile.copy("/tmp/x", "/tmp/new_dir/y")
tf.io.gfile.exists("/tmp/new_dir/y")
True
tf.io.gfile.rmtree("/tmp/new_dir")
If you want to prevent errors if the path already exists, you can use
overwrite
argument:
with open("/tmp/x", "w") as f:
f.write("asdf")
4
tf.io.gfile.copy("/tmp/x", "file:///tmp/y")
tf.io.gfile.copy("/tmp/x", "file:///tmp/y", overwrite=True)
tf.io.gfile.remove("/tmp/y")
Note that the above will still result in an error if you try to overwrite a
directory with a file.
Note that you cannot copy a directory, only file arguments are supported.
Args |
src
|
string, name of the file whose contents need to be copied
|
dst
|
string, name of the file to which to copy to
|
overwrite
|
boolean, if false it's an error for dst to be occupied by an
existing file.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.compat.v1.gfile.Copy\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/lib/io/file_io.py#L585-L587) |\n\nCopies data from `src` to `dst`. \n\n tf.compat.v1.gfile.Copy(\n oldpath, newpath, overwrite=False\n )\n\n with open(\"/tmp/x\", \"w\") as f:\n f.write(\"asdf\")\n\n 4\n tf.io.gfile.exists(\"/tmp/x\")\n True\n tf.io.gfile.copy(\"/tmp/x\", \"/tmp/y\")\n tf.io.gfile.exists(\"/tmp/y\")\n True\n tf.io.gfile.remove(\"/tmp/y\")\n\nYou can also specify the URI scheme for selecting a different filesystem: \n\n with open(\"/tmp/x\", \"w\") as f:\n f.write(\"asdf\")\n\n 4\n tf.io.gfile.copy(\"/tmp/x\", \"file:///tmp/y\")\n tf.io.gfile.exists(\"/tmp/y\")\n True\n tf.io.gfile.remove(\"/tmp/y\")\n\nNote that you need to always specify a file name, even if moving into a new\ndirectory. This is because some cloud filesystems don't have the concept of a\ndirectory. \n\n with open(\"/tmp/x\", \"w\") as f:\n f.write(\"asdf\")\n\n 4\n tf.io.gfile.mkdir(\"/tmp/new_dir\")\n tf.io.gfile.copy(\"/tmp/x\", \"/tmp/new_dir/y\")\n tf.io.gfile.exists(\"/tmp/new_dir/y\")\n True\n tf.io.gfile.rmtree(\"/tmp/new_dir\")\n\nIf you want to prevent errors if the path already exists, you can use\n`overwrite` argument: \n\n with open(\"/tmp/x\", \"w\") as f:\n f.write(\"asdf\")\n\n 4\n tf.io.gfile.copy(\"/tmp/x\", \"file:///tmp/y\")\n tf.io.gfile.copy(\"/tmp/x\", \"file:///tmp/y\", overwrite=True)\n tf.io.gfile.remove(\"/tmp/y\")\n\nNote that the above will still result in an error if you try to overwrite a\ndirectory with a file.\n\nNote that you cannot copy a directory, only file arguments are supported.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|-------------------------------------------------------------------------------|\n| `src` | string, name of the file whose contents need to be copied |\n| `dst` | string, name of the file to which to copy to |\n| `overwrite` | boolean, if false it's an error for `dst` to be occupied by an existing file. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|----------------------------------------------------------------------------------|-------------------------|\n| [`errors.OpError`](https://www.tensorflow.org/api_docs/python/tf/errors/OpError) | If the operation fails. |\n\n\u003cbr /\u003e"]]